这段代码
NSDateComponents *component = [[NSDateComponents alloc] init];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[component setMonth:1];
[component setYear:2013];
[component setDay:1];
currentMonth = [gregorian dateFromComponents:component];
NSLog(@"Date in MonthView %@ ",currentMonth);给我以下日期
Date in MonthView 2012-12-31 23:00:00 +0000为什么?月的范围应该是从0到11?
发布于 2013-09-10 13:14:36
不,从1个月到12个月。这可能是因为你的设备/个人电脑的时区。设置时间线,你就会得到你所期望的日期。
[component setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]]发布于 2013-09-10 13:13:48
实际上,答案可能取决于您正在使用的日历(我看到您使用的是Gregorian,但您的用户可能不使用)。
知道给定日期组件的范围的最安全方法是查询日历。还请注意,它可能会有所不同(例如,公历中的最小天数范围为1-28天,而最大天数为1-31天)。
NSRange minimumMonthRange = [yourCalendar minimumRangeOfUnit:NSMonthCalendarUnit];原始答案
根据“日期和时间规划指南”中的例子:
..。年份单位是2004年,月份单位是5,日单位是6(在公历中,这是2004年5月6日)。
因为飞蛾是一月,二月,三月,四月,五月,.他们用5作为五月,我想说月份的范围是1到12个月。
https://stackoverflow.com/questions/18719644
复制相似问题