Search code examples
iosnscalendar

NSCalendar months range


This code

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);

gives to me the following date

Date in MonthView 2012-12-31 23:00:00 +0000

Why? The months range is supposed to go from 0 to 11 ?


Solution

  • No, months go from 1 to 12. That's probably because the timezone of your device/PC. Set the timezine and you should have the date as you expect

    [component setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]]