[英]Create a timestamp in objective-c like in javascript
在目标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位时间戳?
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];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.