问题
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