从 dateFormat 获取错误的日期
Getting Wrong Date from dateFormat
对于日期输入“00/02/02”
格式样式为 yy/MM/dd
我得到正确的输出,如 02/01/2000
但问题是尝试使用“00/01/01”时
得到这样的输出 '01/01/12100'
但是我不知道为什么今年会这样 12100
我的密码是
let str = "00/01/01"
let inputFormatter = DateFormatter()
inputFormatter.dateFormat = "yy/MM/dd"
if let showDate = inputFormatter.date(from: str) {
inputFormatter.dateFormat = "dd/MM/yyyy"
let resultString = inputFormatter.string(from: showDate)
print(resultString)
}
年份输入类型始终为 yy 格式。
根据@MartinR 的建议
将inputFormatter.defaultDate设置为当前日期或日期(timeIntervalSinceReferenceDate:0)它工作正常
let str = "00/01/01"
let inputFormatter = DateFormatter()
inputFormatter.defaultDate = Date(timeIntervalSinceReferenceDate: 0)
inputFormatter.dateFormat = "yy/MM/dd"
if let showDate = inputFormatter.date(from: str) {
inputFormatter.dateFormat = "dd/MM/yyyy"
let resultString = inputFormatter.string(from: showDate)
print(resultString)
}
我设法通过将格式化程序的时区设置为您当地的时区来重现此错误,然后再从中获取日期:
inputFormatter.timeZone = TimeZone(identifier: "Asia/Kolkata")
//Or
inputFormatter.timeZone = TimeZone(identifier: "Asia/Calcutta")
它们都导致 01/01/12100
。
实际上,使用 yy/MM/dd hh:mm:ss
的日期格式,从 00/01/01 00:00:00
到 00/01/01 05:29:59
的所有日期都给出 12100
的年份部分。这是由于加尔各答的时区与格林威治标准时间相差 +05H30。这是一个错误。
将时区设置为 UTC 会产生所需的输出:
inputFormatter.timeZone = TimeZone(identifier: "UTC") //01/01/2000
这个错误也发生在其他时区:
inputFormatter.timeZone = TimeZone(identifier: "Africa/Addis_Ababa")
inputFormatter.timeZone = TimeZone(identifier: "Europe/Moscow")
inputFormatter.timeZone = TimeZone(identifier: "Asia/Hong_Kong")
基本上所有有 GMT 的时区 + hh:mm
对于日期输入“00/02/02”
格式样式为 yy/MM/dd
我得到正确的输出,如 02/01/2000
但问题是尝试使用“00/01/01”时 得到这样的输出 '01/01/12100'
但是我不知道为什么今年会这样 12100
我的密码是
let str = "00/01/01"
let inputFormatter = DateFormatter()
inputFormatter.dateFormat = "yy/MM/dd"
if let showDate = inputFormatter.date(from: str) {
inputFormatter.dateFormat = "dd/MM/yyyy"
let resultString = inputFormatter.string(from: showDate)
print(resultString)
}
年份输入类型始终为 yy 格式。
根据@MartinR 的建议 将inputFormatter.defaultDate设置为当前日期或日期(timeIntervalSinceReferenceDate:0)它工作正常
let str = "00/01/01"
let inputFormatter = DateFormatter()
inputFormatter.defaultDate = Date(timeIntervalSinceReferenceDate: 0)
inputFormatter.dateFormat = "yy/MM/dd"
if let showDate = inputFormatter.date(from: str) {
inputFormatter.dateFormat = "dd/MM/yyyy"
let resultString = inputFormatter.string(from: showDate)
print(resultString)
}
我设法通过将格式化程序的时区设置为您当地的时区来重现此错误,然后再从中获取日期:
inputFormatter.timeZone = TimeZone(identifier: "Asia/Kolkata")
//Or
inputFormatter.timeZone = TimeZone(identifier: "Asia/Calcutta")
它们都导致 01/01/12100
。
实际上,使用 yy/MM/dd hh:mm:ss
的日期格式,从 00/01/01 00:00:00
到 00/01/01 05:29:59
的所有日期都给出 12100
的年份部分。这是由于加尔各答的时区与格林威治标准时间相差 +05H30。这是一个错误。
将时区设置为 UTC 会产生所需的输出:
inputFormatter.timeZone = TimeZone(identifier: "UTC") //01/01/2000
这个错误也发生在其他时区:
inputFormatter.timeZone = TimeZone(identifier: "Africa/Addis_Ababa")
inputFormatter.timeZone = TimeZone(identifier: "Europe/Moscow")
inputFormatter.timeZone = TimeZone(identifier: "Asia/Hong_Kong")
基本上所有有 GMT 的时区 + hh:mm