Unix 时间戳未正确转换为 NSDate
我已经解析了一个 api,它返回最后更新值的 unixtimestamp。我正在尝试将其转换为 NSDate。代码如下
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[[[feedCount objectAtIndex:i] objectForKey:@"firstitemmsec"] intValue]];
NSLog(@"%@", date);
当我 NSLog 日期时,我得到这样的日期:
2038-01-19 03:14:07 +0000
上面的日期显然是错误的
上面的代码中的错误是什么?
编辑:我的unixtimestamp是
"firstitemmsec":"1264396813500"
这个值对于int来说显然更大。那么我怎样才能最好地处理这种情况
I have parsed an api which returns a unixtimestamp for last updated value. I am trying to convert it into NSDate. The code is below
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[[[feedCount objectAtIndex:i] objectForKey:@"firstitemmsec"] intValue]];
NSLog(@"%@", date);
When I NSLog the date, I am getting date like this:
2038-01-19 03:14:07 +0000
The above date is obviously wrong
What is the mistake in the above code?
EDIT: my unixtimestamp is
"firstitemmsec":"1264396813500"
This value is obviously bigger for int. So how best can I handle this situation
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Unix 时间戳以秒为单位,您拥有的值看起来像是自 1970 年 1 月 1 日以来的毫秒数。如果除以 1000,您将得到 1264396813,根据 此转换器是:
Unix timestamps are in seconds, the value you have looks like a number of milliseconds since 1st January 1970. If you divide by 1000, you get 1264396813, which according to this converter is:
检查
[[[feedCount objectAtIndex:i] objectForKey:@"firstitemmsec"] intValue]
的值 - 很有可能它不是有效的 UNIX 时间戳(好吧,这是因为任何int
值在 iOS 上,但显然它不是正确的值。)Check the value of
[[[feedCount objectAtIndex:i] objectForKey:@"firstitemmsec"] intValue]
--odds seem good that it's not a valid UNIX timestamp (okay, it is since anyint
value is on iOS, but obviously it's not the right value.)我想还有另一件事要做。删除号码末尾的“500”,这是来自服务器的错误代码,并且您有正确的 unix 时间;)
您的服务器上可能有错误或错误...???
I think there's another to do. Erase the "500" at the end of your number wich is an error code from the server and you have a right unix time ;)
There was probably an error or a bug on your server... ???