简体   繁体   中英

Create a timestamp in objective-c like in javascript

In objective c:

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

in Javascript:

new Date().getTime();

The problem is that in objective c the timestamp consists of 10 numbers, however in javascript it has 13 numbers. When I compare the difference of both is within 15 minutes I get always false.

Any ideas how to get a 13 digit timestamp in objective c?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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