Date 在swift 3中使用特定日期的日期格式时日期不匹配
如果日期为2017年3月31日晚上11:59:59,如果我使用Date 在swift 3中使用特定日期的日期格式时日期不匹配,date,swift3,ios10,nsdateformatter,milliseconds,Date,Swift3,Ios10,Nsdateformatter,Milliseconds,如果日期为2017年3月31日晚上11:59:59,如果我使用 var dateFormatter: DateFormatter = { let formatter = DateFormatter() formatter.dateFormat = "MMM d,yyyy HH:mm:ss a" return formatter }() 上述格式和转换秒数后,秒数表示日期2017年3月31日12:59:59 PM cellb
var dateFormatter: DateFormatter =
{
let formatter = DateFormatter()
formatter.dateFormat = "MMM d,yyyy HH:mm:ss a"
return formatter
}()
上述格式和转换秒数后,秒数表示日期2017年3月31日12:59:59 PM
cellbidEndseconds = (Int)((dateFormatter.date(from:arrMap_biddingDateEnd[indexPath.row])?.timeIntervalSince1970)!)
错误在哪里,有人能找出并更新吗?这不是将
Double
转换为Int
的方法。此外,timeIntervalSince1970
返回自1970年1月1日午夜以来的秒数。您需要将其乘以1000以获得毫秒:
if let date = dateFormatter.date(from: arrMap_biddingDateEnd[indexPath.row]) {
cellbidEndMillis = Int(date.timeIntervalSince1970 * 1000)
} else {
print("Cannot convert date")
}
另外:如果您调用
print(date)
,它总是以格林尼治标准时间(GMT)打印日期,与您的当地时间相比,格林尼治标准时间可能要晚几个小时。你需要在心里把它转换成你的本地时区(或者写一个方法来帮你)。这是我在StackOverflow上看到的一大混乱源。12小时格式是hh
您忽略了应同时设置的区域设置和本地时区。@即使设置了区域设置和本地时区,结果也没有变化also@vadian如果我使用hh,,,EXC_断点在我将日期转换为毫秒的行崩溃,然后调整代码以转换毫秒。无论如何,hh
解决了错误的时间。实际上那只是秒,变量名我更新了。通过“如果让”转换为秒是我同意的最佳方式。但是,如果我不确定我是否会得到约会,那么我必须使用。但我很确定我会从数组中得到日期。那就没什么区别了。我也试过用当地时区和地区来检查。结果没有差别。