iOS - NSDateFormatter dateFromString returns 仅在一种情况下为零
iOS - NSDateFormatter dateFromString returns nil only in one case
我正在测试日期格式的一些值 ("dd-MM-yyyy"
),有一个特殊情况我无法解释:
var datef = NSDateFormatter()
datef.dateFormat = "dd-MM-yyyy";
var date_a = "02-01-1990"
var date_b = "01-01-1990"
var date_f_a = datef.dateFromString(date_a);
var date_f_b = datef.dateFromString(date_b);
data_f_a
returns 1990 年 1 月 2 日,12:00 上午,但 date_f_b
returns 为零。除 1990 年 1 月 1 日外,任何其他日期都将 return 为预期值。
如果我添加 datef.lenient = true
date_f_b 不再是零,但我不需要这样做。为什么它是无效日期?
编辑 1:
如果我使用 DateFormatter()
:
也会发生同样的情况
编辑 2:
Xcode版本:8.1
对于xCode 7.3
你需要使用 NSDateComponents
var datef = NSDateFormatter()
datef.dateFormat = "dd-MM-yyyy";
var dtCompo = NSDateComponents()
dtCompo.day = 2
dtCompo.month = 1
dtCompo.year = 1990
let cal = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
var date_f_a = cal.dateFromComponents(dtCompo)
dtCompo = NSDateComponents()
dtCompo.day = 1
dtCompo.month = 1
dtCompo.year = 1990
var date_f_b = cal.dateFromComponents(dtCompo)
var date_f_a_string = datef.stringFromDate(date_f_a!)
var date_f_b_string = datef.stringFromDate(date_f_b!)
print(date_f_a_string)
print(date_f_b_string)
输出将
xCode8.1
var datef = DateFormatter()
datef.dateFormat = "dd-MM-yyyy";
var dtCompo = DateComponents()
dtCompo.day = 2
dtCompo.month = 1
dtCompo.year = 1990
let cal = NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian)!
var date_f_a = cal.date(from: dtCompo)
dtCompo = DateComponents()
dtCompo.day = 1
dtCompo.month = 1
dtCompo.year = 1990
var date_f_b = cal.date(from: dtCompo)
var date_f_a_string = datef.string(from: date_f_a!)
var date_f_b_string = datef.string(from: date_f_b!)
print(date_f_a_string)
print(date_f_b_string)
输出将
经过一些评论,确定问题中的代码是运行,区域设置为es_PE
。这是秘鲁的国家。有问题的日期是 1990 年 1 月 1 日。默认情况下,NSDateFormatter
使用本地时区,并且在解析没有时间的日期字符串时,假设为午夜。
在秘鲁,1990 年,夏令时从 1990 年 1 月 1 日午夜开始。这意味着时钟从 1989 年 12 月 31 日 11:59:59pm 直接转到 1990 年 1 月 1 日 1:00:00am。 1990年1月1日没有午夜
这就是此用户尝试转换字符串 01-01-1990
失败的原因。秘鲁的这个日期没有午夜(可能还有一些其他地方,如果有的话,同时开始夏令时)。大多数测试此代码的人会声称它工作得很好,因为大多数测试此代码的人不住在秘鲁。
我找到了一个包含有用信息的有用网站。有关秘鲁和夏令时的详细信息,请参阅 http://www.timeanddate.com/time/change/peru/lima?year=1990。请注意,在 1989 年和 1991 年,秘鲁没有使用夏令时。
我正在测试日期格式的一些值 ("dd-MM-yyyy"
),有一个特殊情况我无法解释:
var datef = NSDateFormatter()
datef.dateFormat = "dd-MM-yyyy";
var date_a = "02-01-1990"
var date_b = "01-01-1990"
var date_f_a = datef.dateFromString(date_a);
var date_f_b = datef.dateFromString(date_b);
data_f_a
returns 1990 年 1 月 2 日,12:00 上午,但 date_f_b
returns 为零。除 1990 年 1 月 1 日外,任何其他日期都将 return 为预期值。
如果我添加 datef.lenient = true
date_f_b 不再是零,但我不需要这样做。为什么它是无效日期?
编辑 1:
如果我使用 DateFormatter()
:
编辑 2: Xcode版本:8.1
对于xCode 7.3 你需要使用 NSDateComponents
var datef = NSDateFormatter()
datef.dateFormat = "dd-MM-yyyy";
var dtCompo = NSDateComponents()
dtCompo.day = 2
dtCompo.month = 1
dtCompo.year = 1990
let cal = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
var date_f_a = cal.dateFromComponents(dtCompo)
dtCompo = NSDateComponents()
dtCompo.day = 1
dtCompo.month = 1
dtCompo.year = 1990
var date_f_b = cal.dateFromComponents(dtCompo)
var date_f_a_string = datef.stringFromDate(date_f_a!)
var date_f_b_string = datef.stringFromDate(date_f_b!)
print(date_f_a_string)
print(date_f_b_string)
输出将
var datef = DateFormatter()
datef.dateFormat = "dd-MM-yyyy";
var dtCompo = DateComponents()
dtCompo.day = 2
dtCompo.month = 1
dtCompo.year = 1990
let cal = NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian)!
var date_f_a = cal.date(from: dtCompo)
dtCompo = DateComponents()
dtCompo.day = 1
dtCompo.month = 1
dtCompo.year = 1990
var date_f_b = cal.date(from: dtCompo)
var date_f_a_string = datef.string(from: date_f_a!)
var date_f_b_string = datef.string(from: date_f_b!)
print(date_f_a_string)
print(date_f_b_string)
输出将
经过一些评论,确定问题中的代码是运行,区域设置为es_PE
。这是秘鲁的国家。有问题的日期是 1990 年 1 月 1 日。默认情况下,NSDateFormatter
使用本地时区,并且在解析没有时间的日期字符串时,假设为午夜。
在秘鲁,1990 年,夏令时从 1990 年 1 月 1 日午夜开始。这意味着时钟从 1989 年 12 月 31 日 11:59:59pm 直接转到 1990 年 1 月 1 日 1:00:00am。 1990年1月1日没有午夜
这就是此用户尝试转换字符串 01-01-1990
失败的原因。秘鲁的这个日期没有午夜(可能还有一些其他地方,如果有的话,同时开始夏令时)。大多数测试此代码的人会声称它工作得很好,因为大多数测试此代码的人不住在秘鲁。
我找到了一个包含有用信息的有用网站。有关秘鲁和夏令时的详细信息,请参阅 http://www.timeanddate.com/time/change/peru/lima?year=1990。请注意,在 1989 年和 1991 年,秘鲁没有使用夏令时。