简体   繁体   English

在javascript中像在Objective-C中创建一个时间戳

[英]Create a timestamp in objective-c like in javascript

In objective c: 在目标c中:

NSDate *past = [NSDate date];
NSTimeInterval oldTime = [past timeIntervalSince1970];
NSString *timestamp = [[NSString alloc] initWithFormat:@"%0.0f", oldTime];

in Javascript: 在Javascript中:

new Date().getTime();

The problem is that in objective c the timestamp consists of 10 numbers, however in javascript it has 13 numbers. 问题是在目标c中,时间戳由10个数字组成,但是在javascript中,它有13个数字。 When I compare the difference of both is within 15 minutes I get always false. 当我比较两者的差异在15分钟之内时,我总是会错误的。

Any ideas how to get a 13 digit timestamp in objective c? 有什么想法如何在目标c中获得13位时间戳?

getTime() http://www.w3schools.com/jsref/jsref_gettime.asp getTime() http://www.w3schools.com/jsref/jsref_gettime.asp

Returns the number of milliseconds since 1970/01/01:

- (NSTimeInterval)timeIntervalSince1970

Returns the interval between the receiver and the first instant of 1 January 1970, GMT.
NSTimeInterval used to specify a time interval, in seconds.

So you need to multiply value by 1000 to convert seconds to milliseconds 因此,您需要将值乘以1000才能将秒转换为毫秒

NSDate *past = [NSDate date];
NSTimeInterval oldTime = [past timeIntervalSince1970];
NSString *timestamp = [[NSString alloc] initWithFormat:@"%0.0f", oldTime * 1000];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM