0

I am retrieving information from an api and I am retrieving a value which holds a time, but the time looks like this, 1469880000 I did some research and realized that this is Epoch time. Now I wonder if you can make a mathmatical equation to convert Epoch to regular time. In there a function or framework in swift to so this? I want to turn this, 1469880000 into this H:MM -- .

Paul
  • 94
  • 9

2 Answers2

0

NSDate has a init overload method that works with EPOCH time.

import Foundation
let myDate = NSDate.init(timeIntervalSince1970: 1469880000)
print(myDate)
Alan
  • 45,915
  • 17
  • 113
  • 134
0

As Alan notes you can use the overloaded method, here's an example.

let date = NSDate(timeIntervalSince1970: 1469880000)

Then to form it you can do something like the following...

let formatString = "yyyy-MM-dd'T'HH:mm:ss.sss'Z'"

func convertTimestampToUTC(inDate:NSDate,formatString:String) -> String{
    let formatter = NSDateFormatter()
    formatter.dateFormat = formatString
    formatter.timeZone = NSTimeZone(abbreviation: "UTC")
    let outDate = formatter.stringFromDate(inDate)
    return outDate
}

Then use it like so,

let myFormattedDate = convertTimestampToUTC(date, formatString:formatString)

I'll update this shortly with some sample output once I get a moment, hope this helps!