日期格式不正确?
Incorrect date formatting?
请帮助我解决不明显的代码行为
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
dateComponents.hour = 15;
dateComponents.minute = 20;
dateComponents.calendar = calendar;
NSDate * endDate = [calendar dateFromComponents:dateComponents];
结束日期为 0001-01-01 12:49:43 +0000。为什么?
由于时区不同,小时值可能不正确。为什么会有如此奇怪的分秒值?
问候。
如果我们只是考虑下面的代码片段
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setCalendar:calendar];
NSDate *date = [calendar dateFromComponents:components];
date = 0000-12-31 23:06:32 +0000
, 因为:
Calendars encapsulate information about systems of reckoning time in
which the beginning, length, and divisions of a year are defined.
[...]
When there are insufficient components provided to completely specify an absolute time, a calendar uses default values of its choice.
Apple Documentation
和
An NSDateComponents
object is meaningless in itself; you need to know what calendar it is interpreted against, and you need to know whether the values are absolute values of the units, or quantities of the units. Apple Documentation
实际上,一切都很好,正如文档所说的那样。
请帮助我解决不明显的代码行为
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
dateComponents.hour = 15;
dateComponents.minute = 20;
dateComponents.calendar = calendar;
NSDate * endDate = [calendar dateFromComponents:dateComponents];
结束日期为 0001-01-01 12:49:43 +0000。为什么?
由于时区不同,小时值可能不正确。为什么会有如此奇怪的分秒值? 问候。
如果我们只是考虑下面的代码片段
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setCalendar:calendar];
NSDate *date = [calendar dateFromComponents:components];
date = 0000-12-31 23:06:32 +0000
, 因为:
Calendars encapsulate information about systems of reckoning time in which the beginning, length, and divisions of a year are defined.
[...]
When there are insufficient components provided to completely specify an absolute time, a calendar uses default values of its choice. Apple Documentation
和
An
NSDateComponents
object is meaningless in itself; you need to know what calendar it is interpreted against, and you need to know whether the values are absolute values of the units, or quantities of the units. Apple Documentation
实际上,一切都很好,正如文档所说的那样。