I am using this method to convert a month and year to a date which equals the last day in the month of the year given.
+ (NSDate*)endOfMonthDateForMonth:(int)month year:(int)year
{
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [[NSDateComponents alloc] init];
comps.year = year;
comps.month = month;
NSDate *monthYearDate = [calendar dateFromComponents:comps];
// daysRange.length will contain the number of the last day of the endMonth:
NSRange daysRange = [calendar rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:monthYearDate];
comps.day = daysRange.length;
comps.hour = 0;
comps.minute = 0;
comps.second = 0;
[comps setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[calendar setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[calendar setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
NSDate *endDate = [calendar dateFromComponents:comps];
return endDate;
}
I want the date to have a time component of 00:00:00, thats why I have set the time zone to GMT 0 and the date components for minutes, hours and seconds to 0. The date returned from the method is correct and has a time component from 00:00:00.
This is how I save the date to Core Data:
NSDate *endDate = [IBEstPeriod endOfMonthDateForMonth:endMonth year:endCalYear];
[annualPeriod setEndDate:endDate];
After retrieving the data and NSLogging it to the debugger console, I get dates like 2008-12-30 23:00:00 +0000
with a time component != 0.
Why did the component change now? Shouldn't it stay at 00:00:00?
What did I code wrong here?
Thank you!!