Note that there are some explanatory texts on larger screens.

plurals
  1. POTime zone issue when retrieving an NSDate from Core Data
    primarykey
    data
    text
    <p>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.</p> <pre><code>+ (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; } </code></pre> <p>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.</p> <p>This is how I save the date to Core Data:</p> <pre><code>NSDate *endDate = [IBEstPeriod endOfMonthDateForMonth:endMonth year:endCalYear]; [annualPeriod setEndDate:endDate]; </code></pre> <p>After retrieving the data and NSLogging it to the debugger console, I get dates like <code>2008-12-30 23:00:00 +0000</code> with a time component != 0. </p> <p>Why did the component change now? Shouldn't it stay at 00:00:00? </p> <p>What did I code wrong here? </p> <p>Thank you!!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload