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

查看:28
本文介绍了将 .NET 中的 DateTime 对象格式化为 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??

谢谢!

推荐答案

来自 解析 JSON 日期iPhone 我觉得下面这个功能很完美:

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];
}

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

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