将DateTime对象从.NET格式化为用于objective-c的NSDate [英] Formatting a DateTime object from .NET into a NSDate for objective-c

查看:107
本文介绍了将DateTime对象从.NET格式化为用于objective-c的NSDate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个将.NET DateTime对象返回到我的iOS应用程序的API。我有点困惑,发生什么事情,DateTime看起来很好,当它离开API,但是当它进来它通过JSON,并作为一个字符串,如下所示:

I am working with an API that returns a .NET DateTime object into my iOS application. I'm a little confused at what's going on, the DateTime looks fine when it leaves the API, but when it comes in it goes through JSON and comes in as a string that looks like this:

/Date(1303884000000-0600)/

WTF是那个,我该怎么把它变成NSDate对象?

WTF is that and how can I turn it into a NSDate object??

谢谢!

推荐答案

在iPhone上解析JSON日期我发现以下功能要完美:

From Parsing JSON dates on IPhone I found the following function to be perfect:

    - (NSDate*) getDateFromJSON:(NSString *)dateString
{
// Expect date in this format "/Date(1268123281843)/"
int startPos = [dateString rangeOfString:@"("].location+1;
int endPos = [dateString rangeOfString:@")"].location;
NSRange range = NSMakeRange(startPos,endPos-startPos);
unsigned long long milliseconds = [[dateString substringWithRange:range] longLongValue];
NSLog(@"%llu",milliseconds);
NSTimeInterval interval = milliseconds/1000;
return [NSDate dateWithTimeIntervalSince1970:interval];
}

这篇关于将DateTime对象从.NET格式化为用于objective-c的NSDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆