到达日期的毫秒数在Swift 3中给出了错误的日期

时间:2018-02-07 17:32:23

标签: swift date

我从URL请求

以毫秒的形式获取日期

这是JSON字典中的这种形式:

AllocationDate =" / Date(1514485800000)/&#34 ;;

我使用以下代码将其转换为yyyy-MM-dd格式:

let data4 = data_object1["AllocationDate"] as! String
let start = data4.index(data4.startIndex, offsetBy: 6)  
let end = data4.index(data4.endIndex, offsetBy: -2)
let range = start..<end
let milliseconds = Double(data4.substring(with: range))
let date = Date(timeIntervalSince1970: (milliseconds!/1000))
let dateString = String(describing: date)
print(dateString)

现在,它打印2017-12-28 18:30:00 +0000,而实际日期是2017-12-29。

检查:https://www.timecalculator.net/milliseconds-to-date

因此每个日期都会转换为实际日期的上一个日期。

请帮助我。如果我在我的代码或其他方面犯了一些错误。 谢谢!

0 个答案:

没有答案