【发布时间】:2021-03-10 07:55:18
【问题描述】:
我正在使用以下代码来解析时间戳值。但是当我将它转换为日期时,它给出了正确的值(03-08-2021),但后来我尝试将此日期转换为字符串以满足我的要求。
但是该字符串导致了错误的值 03-07-2021。以下是我正在使用的代码。
let epocTime = TimeInterval(1615161600000/1000)
let date = Date(timeIntervalSince1970: epocTime)
print(date)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.timeZone = .current
dateFormatter.locale = .current
let dateString = dateFormatter.string(from: date)
print(dateString)
【问题讨论】:
-
纪元时间戳通常采用 UTC 格式,
print始终以 UTC 格式显示日期。将第一行print替换为print(date.description(with: .current))。现在日期表示将匹配。
标签: ios swift nsdateformatter