我有一个 API 调用要求我将日期转换为以毫秒为单位的 timeIntervalSince1970
,他们还希望我添加正确的时区差异。所以我的代码现在是:
if let timeStamp = incident?.publishedTimeStamp as Date? {
let roundedDown = Int(timeStamp.timeIntervalSince1970 * 1000)
publishedDate = String(roundedDown)
}
这给了我一个数字,如果我在这里插入它: https://www.epochconverter.com/
现在如您所见,GMT
时间(最上面的结果)是我目前实现的。我需要添加足够多的毫秒数,以便 您的时区 部分位于 GMT
部分。我希望这是有道理的。
那么我如何才能获得代表当前时区与 GMT
之间差异的毫秒数,以便我可以将其与当前结果相加?
最佳答案
var roundedDown = Double(TimeZone.current.secondsFromGMT(for: timeStamp)) + timeStamp.timeIntervalSince1970
roundedDown *= 1000
publishedDate = String(Int(roundedDown))
关于swift - swift 将当前时区差异添加到 timeintervalsince1970,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46522436/