[英]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.