Search code examples
iosdatensdatenscalendarnsdatecomponents

NSDateComponents from NSDate yielding unexpected results


I have an NS date stored that is logged as

2013-03-15 00:00:01 +0000

I presume that the +0000 is UTC time. However, when I try

NSCalendar* theCalendar = [NSCalendar currentCalendar];

unsigned theUnitFlags = NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;

NSDateComponents* compsForDate = [theCalendar components:theUnitFlags fromDate:compDate];

The comps are logged as:

<NSDateComponents: 0xc0537b0>
    Calendar Year: 2013
    Month: 3
    Leap month: no
    Day: 14
    Hour: 20
    Minute: 0
    Second: 1
    Weekday: 5

I would have assumed that the day would be 15 and the hour would be 0, since I didn't think there was a timezone associated with this date. Can anyone tell me where I am going wrong?


Solution

  • There is a time zone associated with that date-UTC. There is also a time zone associated with your calendar; probably the time zone from your locale settings (that's the default anyway). The calendar is giving you the date components that arise from interpreting that date in its time zone.