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

…衆ロ難τιáo~ 提交于 2021-02-10 16:11:45

问题


I am getting something strange date/time from server.

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


回答1:


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)



回答2:


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)
   }
}


来源:https://stackoverflow.com/questions/45189539/how-to-convert-this-unix-epoch-date-in-milliseconds-to-local-date-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!