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

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.


update: Xcode 8.2.1 • Swift 3.0.2 or later

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

let epochTime = TimeInterval(1429162809359) / 1000
let date = Date(timeIntervalSince1970: epochTime)   // "Apr 16, 2015, 2:40 AM"

print("Converted Time \(date)")         // "Converted Time 2015-04-16 05:40:09 +0000\n"

Swift 5

I am dealing with a date in a JSON api which is defined as an Int and an example of the timestamp is 1587288545760 (UTC)

The only way I could display that value as a Date in a way that made any sense was to truncate the last 3 digits and convert THAT resultant date to "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"

This was the function I created to achieve that.

func convertDate(dateValue: Int) -> String {
    let truncatedTime = Int(dateValue / 1000)
    let date = Date(timeIntervalSince1970: TimeInterval(truncatedTime))
    let formatter = DateFormatter()
    formatter.timeZone = TimeZone(abbreviation: "UTC")
    formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
    return formatter.string(from: date)
}

It works for me and I end up with a date that looks like this:

"2020-04-19T09:29:05.000Z"

..and it reflects the fact that the original time stamp is exactly that date.

Hope that helps anyone having the same issue.