Search code examples
iosswiftdatetimestampnsdateformatter

Date From Timestamp Becomes Wrong


I am trying to convert timestamp to Date. But it returns wrong date.

I have a time stamp

1524637838000.0

Which returns 25-04-2018 12:00 as per this online converter

But I get wrong date when convert using my code

let date = Date(timeIntervalSince1970:1524637838000.0)
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "dd-MM-yyyy hh:mm"
    let myDate = dateFormatter.string(from: date)
    print("Date is = ",myDate)

I get

07-11-50283 12:03

as result. Is there anything wrong with my code?


Solution

  • your timestamp 1524637838000.0 in milliseconds, it should be in seconds, so your date initialization should be:

    let date = Date(timeIntervalSince1970:(1524637838000.0/1000))