在objective-c中创建时间戳,就像在javascript中一样

Create a timestamp in objective-c like in javascript

本文关键字:一样 javascript objective-c 创建 时间戳      更新时间:2023-09-26

In objective c:

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

new Date().getTime();

问题是在objective c中时间戳由10个数字组成,然而在javascript中它有13个数字。当我将两者的差异在15分钟内进行比较时,我总是得到错误。

任何想法如何在目标c中获得13位时间戳?

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.

你需要将value乘以1000来将秒转换为毫秒

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