Convert milliseconds to NSDate

不想你离开。 提交于 2019-11-26 08:29:55

问题


I have this \"1273636800000\" and I want to convert it to this format \"Wed Mar 03 2010 00:00:00 GMT-0500 (EST)\"

I need to convert this milliseconds to NSDate format.

I tried this

NSDate *tr = [NSDate dateWithTimeIntervalSince1970:1273636800000];

and

NSDate *tr = [NSDate dateWithTimeIntervalSinceReferenceDate:1273636800000];

and

NSDate *tr = [NSDate dateWithTimeIntervalSinceNow:1273636800000];

But I can\'t get it to work, anyone had a similar problem and found the solution


回答1:


These functions (dateWithTimeIntervalSince1970, dateWithTimeIntervalSinceReferenceDate, dateWithTimeIntervalSinceNow) accept parameter in seconds.

NSDate *date = [NSDate dateWithTimeIntervalSince1970:(ms / 1000.0)];

You should pass 1273636800.000

NSDate *date = [NSDate dateWithTimeIntervalSince1970:(1273636800 / 1000.0)];



回答2:


Divide by 1000 to get millis in seconds, that's what you want.




回答3:


A simple one-line Swift 2 function:

func dateFromMilliseconds(ms: NSNumber) -> NSDate {
    return NSDate(timeIntervalSince1970:Double(ms) / 1000.0)
}


来源:https://stackoverflow.com/questions/2741199/convert-milliseconds-to-nsdate

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