How to Convert UNIX epoc time to Date and time in ios swift

爷,独闯天下 提交于 2019-11-26 14:58:41

问题


I'm trying to convert the UNIX epoc time to datetime format using the below code

var epocTime = NSTimeInterval(1429162809359)

let myDate = NSDate(timeIntervalSince1970: epocTime)
println("Converted Time \(myDate)")

the actual result is (Thu, 16 Apr 2015 05:40:09 GMT) but am getting something like (47258-05-14 05:15:59 +0000) Can anyone please tel me how to achieve this.


回答1:


update: Xcode 8.2.1 • Swift 3.0.2

You need to convert it from milliseconds dividing it by 1000:

let epocTime = TimeInterval(1429162809359) / 1000

let myDate = Date(timeIntervalSince1970:  epocTime)   // "Apr 16, 2015, 2:40 AM"
print("Converted Time \(myDate)")         // "Converted Time 2015-04-16 05:40:09 +0000\n"



回答2:


It seems that your time information is "milliseconds since 1970". Should have been straightforward to figure out: We are about 46 years after 1970, but your date is about 45,000 after 1970.

NSTimeInterval is based on SECONDS.

Convert your number to double and divide by 1000.0.



来源:https://stackoverflow.com/questions/29670585/how-to-convert-unix-epoc-time-to-date-and-time-in-ios-swift

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