MwcsMac MwcsMac - 2 years ago 314
Swift Question

Swift convert unix time to date and time

My current code.

if let var timeResult = (jsonResult["dt"] as? Double) {
timeResult = NSDate().timeIntervalSince1970

The results

println(timeResult) = 1415639000.67457

println(NSdate()) = 2014-11-10 17:03:20 +0000 was just to test to see what NSdate was providing.

I want the first to look like the last. The value for dt = 1415637900.

Also how can I adjust for time zone? Running on iOS.

Answer Source

You can get a date with that value by using the NSDate(withTimeIntervalSince1970:) initializer:

let date = NSDate(timeIntervalSince1970: 1415637900)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download