Reputation: 14925
I am trying to parse out the day of the week given a NSDate.
NSDateComponents *comps = [[NSDateComponents alloc] init];
comps=[calendar components: NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitWeekOfMonth | NSCalendarUnitWeekOfYear | NSCalendarUnitWeekday fromDate:date];
When the value of date is 2015-11-30 00:58:52 +0000
(this is Monday), the value of comps.weekday
is printed out as 1
When the value of date as 2015-11-15 09:59:46 +0000
(this is Sunday), the value of comps.weekday
is also 1.
Upvotes: 3
Views: 1017
Reputation: 4813
NSDateComponents
uses the timezone of the NSCalendar
to interpret the current day. NSCalendar
defaults to the using the timezone of the device.
2015-11-30 00:58:52 UTC
is only a Monday if you are in the UTC
timezone, or one of the later ones. If you are in the EST
(-5) timezone, then this would be a Sunday.
If you want to interpret the day of week at the UTC
timezone then change the timeZone
property to [NSTimezone timeZoneForSecondsFromGMT:0]
.
Documentation ref: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateComponents_Class/#//apple_ref/occ/instp/NSDateComponents/timeZone
Upvotes: 5