如何将此UNIX纪元日期(以毫秒为单位)转换为本地日期/时间?

时间:2017-07-19 11:43:55

标签: swift nsdateformatter

我从服务器得到一些奇怪的日期/时间。

enter image description here

如何转换“notification_date”:1500461137000, 以当地时间格式。

2 个答案:

答案 0 :(得分:4)

这是一个UNIX纪元日期,以毫秒为单位。将其除以1000后,您可以使用timeIntervalSince1970进行转换。

let localDate = Date(timeIntervalSince1970: notificationDate / 1000)

答案 1 :(得分:0)

在Double上进行扩展,并根据您的通知日期进行转换。

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