converting timestamp to nsdate format

橙三吉。 提交于 2019-11-27 01:13:56

问题


I want to convert my timestamp value 1308031456 to NSDate format (which yields the value Tue, 14 Jun 2011 06:04:16 GMT in online web conversion). How would I convert between the two programmatically?


回答1:


OBJ - C: Use dateWithTimeIntervalSince1970:,

NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeStamp];

SWIFT: Use init(timeIntervalSince1970:)

let date = NSDate(timeIntervalSince1970:timeStamp)



回答2:


You can use the use the method dateWithTimeIntervalSince1970: to convert the timestamp you've which is the epoch time.

NSDate * myDate = [NSDate dateWithTimeIntervalSince1970:1308031456];



回答3:


In obj c

double timeStamp = 1513330403393;
NSTimeInterval unixTimeStamp = timeStamp / 1000.0;
NSDate *exactDate = [NSDate dateWithTimeIntervalSince1970:unixTimeStamp];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"dd/MM/yyy hh:mm a";
NSString  *finalate = [dateFormatter stringFromDate:exactDate];

In Swift

let timeStamp = 1513330403393
let unixTimeStamp: Double = Double(timeStamp) / 1000.0
let exactDate = NSDate.init(timeIntervalSince1970: unixTimeStamp)
let dateFormatt = DateFormatter();
dateFormatt.dateFormat = "dd/MM/yyy hh:mm a"
print(dateFormatt.string(from: exactDate as Date))



回答4:


See the various example on date formatting and getting date from timestamp from apple

Use Formatter Styles to Present Dates and Times With the User’s Preferences

Convert TimeStamp to NSDate in Objective-C



来源:https://stackoverflow.com/questions/6407057/converting-timestamp-to-nsdate-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!