我从服务器获取unix时间戳
1589275703283
当我尝试从以下网站进行转换时:https://www.epochconverter.com
我得到Tuesday, 12 May 2020
并且我正在使用这段代码将此时间戳转换为我可以读取的日期
func timeStringFromUnixTime(timestamp: Int64) -> String {
let date = Date(timeIntervalSince1970: TimeInterval(timestamp))
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd, MMMM yyyy"
dateFormatter.timeZone = TimeZone(identifier: NSTimeZone.default.identifier)
let localDate = dateFormatter.string(from: date)
return localDate
}
但是它通过了我22 February 52332
,这显然是错误的
有人可以帮助我解决问题所在吗? 预先感谢
答案 0 :(得分:1)
时间戳“ 1589275703283
”对于“正常”的UNIX时间戳来说太大了。它以毫秒为单位,而不是秒(epochconverter.com
足以检测到;它正在打印“假设此时间戳以毫秒为单位” )。因此,您需要将时间戳除以1000:
let date = Date(timeIntervalSince1970: TimeInterval(timestamp) / 1000)