WCF 日期时间到 NSDate
我正在使用返回 JSON WCF DateTime 的 Web 服务。
该字符串类似于 \/Date(1316397792913+0800)\/
我有兴趣提取 1316397792
这是自 1970 年 1 月 1 日以来的时间(以秒为单位)。这样我就可以使用 NSDate timeIntervalSince1970
方法来获取当前时间。我剪掉了最后 3 位数字,因为它以毫秒为单位,而 timeIntervalSince1970
以秒为单位。
这是我目前正在做的事情,它不适用于 2001 年之前的某个日期,该日期自 1970 年以来的时间间隔(以毫秒为单位)少于 10 个字符。
NSString *dateString = @"\/Date(1316397792913+0800)\/";
NSLog(@"dateString :%@", dateString);
NSDate *date = [NSDate dateWithTimeIntervalSince1970:
[[dateString substringWithRange:NSMakeRange(6, 10)] intValue]];
NSLog(@"NSDate:%@", date);
dateString :/Date(1316397792913+0800)/
NSDate:2011-09-19 02:03:12 +0000
因此,我需要更好地处理 dateString,它并不假设每次我们都会获取 10 个字符。
I am using a web services which returns a JSON WCF DateTime.
The string goes like \/Date(1316397792913+0800)\/
I am interested in extracting out the 1316397792
which is the time since 1st Jan 1970 in seconds. So that I can use the NSDate timeIntervalSince1970
method to get the present time. I cropped out the last 3 digits as it's in milliseconds and the timeIntervalSince1970
takes in seconds.
Here's what I am currently doing which does not work for dates somewhere before 2001 which has a time interval in ms since 1970 less than 10 characters.
NSString *dateString = @"\/Date(1316397792913+0800)\/";
NSLog(@"dateString :%@", dateString);
NSDate *date = [NSDate dateWithTimeIntervalSince1970:
[[dateString substringWithRange:NSMakeRange(6, 10)] intValue]];
NSLog(@"NSDate:%@", date);
dateString :/Date(1316397792913+0800)/
NSDate:2011-09-19 02:03:12 +0000
Therefore, I need a better work around playing with the dateString which does not assume everytime we will be feteching 10 characters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以简单地获取
(
索引和+
索引之间的子字符串。类似于:PS:检查一次性错误。
You can simply get the substring between the index of
(
and the index of+
. Something like:P.S: Check for the one off error.