Search code examples
datetype-conversionswift3epoch

How to convert unix epoche time into Date in Swift3


I want to convert epoche time (number) retrieved from database into a Date Object using Swift 3.

I've got following code for the number

// e.g. 1475616846.424875
user.birthday.timeIntervalSince1970

How can I convert this in a Date object?


Solution

  • Check the reference of Date:

    init(timeIntervalSince1970: TimeInterval) 
    

    Returns a Date initialized relative to 00:00:00 UTC on 1 January 1970 by a given number of seconds.

    Simply use the initializer of Date:

    let epochTime: TimeInterval = 1475616846.424875
    let date = Date(timeIntervalSince1970: epochTime)
    print(date) //->2016-10-04 21:34:06 +0000