I have this code:
NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
[components setYear:2011];
[components setDay:1];
[components setMonth:4];
NSCalendar *gregorianCalendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *firstDate = [gregorianCalendar dateFromComponents:components];
NSLog(@"date:%@", firstDate);
the result in console is:
date:2011-03-31
why???? if I set "setDay:1" it would be "date:2011-04-01", no?
Try:
From the NSDateComponents docs:
I think this could be a time zone related problem. Be sure to set your time zone on NSDateComponents:
Also it depends on the date formatting. My dates were 1 year old for
YYYY
and I change it toyyyy
(lower case) then it was fixed. Be careful about that.