Formatting a DateTime object from .NET into a NSDate for objective-c(将 .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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将 .NET 中的 DateTime 对象格式化为 Objective-c 的 NSDate
基础教程推荐
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
