iDeveloper
iDeveloper

Reputation: 2444

How to convert this UNIX epoch date in milliseconds to local date/time?

I am getting something strange date/time from server.

enter image description here

How to convert "notification_date": 1500461137000, to local time format.

Upvotes: 1

Views: 673

Answers (2)

arvinq
arvinq

Reputation: 699

Do an extension on Double and convert from your notification date.

 Double(notificationDate).convertEpochTime()
 ...
 extension Double {
    func convertEpochTime() -> String{
       let readableDate = Date(timeIntervalSince1970: self / 1000.0)

       let dateFormatter = DateFormatter()
       dateFormatter.dateStyle = .medium
       dateFormatter.dateFormat = "EEEE, MMM d"

       return dateFormatter.string(from: readableDate)
   }
}

Upvotes: 0

glyvox
glyvox

Reputation: 58049

This is a UNIX epoch date in milliseconds. You can convert it with timeIntervalSince1970 after dividing it by 1000.

let localDate = Date(timeIntervalSince1970: notificationDate / 1000)

Upvotes: 4

Related Questions