首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在objective中创建一个时间戳,如javascript中的

在objective中创建一个时间戳,如javascript中的
EN

Stack Overflow用户
提问于 2014-03-26 21:40:09
回答 1查看 343关注 0票数 0

在目标c中:

代码语言:javascript
复制
NSDate *past = [NSDate date];
NSTimeInterval oldTime = [past timeIntervalSince1970];
NSString *timestamp = [[NSString alloc] initWithFormat:@"%0.0f", oldTime];

在Javascript中:

代码语言:javascript
复制
new Date().getTime();

问题是,在目标c中,时间戳由10个数字组成,而在javascript中则有13个数字。当我比较两者的差异是在15分钟内,我总是错误的。

有什么办法在目标c中得到13位数字的时间戳吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-26 21:47:25

getTime() gettime.asp

代码语言:javascript
复制
Returns the number of milliseconds since 1970/01/01:

- (NSTimeInterval)timeIntervalSince1970

代码语言:javascript
复制
Returns the interval between the receiver and the first instant of 1 January 1970, GMT.
NSTimeInterval used to specify a time interval, in seconds.

因此,需要将值乘以1000,才能将秒转换为毫秒。

代码语言:javascript
复制
NSDate *past = [NSDate date];
NSTimeInterval oldTime = [past timeIntervalSince1970];
NSString *timestamp = [[NSString alloc] initWithFormat:@"%0.0f", oldTime * 1000];
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22673645

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档