在javascript中创建objective-c中的时间戳

时间:2014-03-26 21:40:10

标签: javascript objective-c ios7

在目标c:

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

在Javascript中:

new Date().getTime();

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

如何在目标c中获得13位数的时间戳?

1 个答案:

答案 0 :(得分:1)

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.

所以你需要将值乘以1000以将秒转换为毫秒

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