Question

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?

Was it helpful?

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.

OTHER TIPS

The +0000 in the time string indicate the hour offset of UTC (Greenwich, England). The resultant time components you've included in the post are the time with your local time zone factored in. Based on the comps, you local time zone is -4 hours away from UTC.

Kevin.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top