2

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?

user6788419
  • 7,196
  • 1
  • 16
  • 28

1 Answers1

11

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

let date = Date(timeIntervalSince1970:(1524637838000.0/1000))
Moayad Al kouz
  • 1,342
  • 1
  • 9
  • 19