iOS:NSCalendarUnitWeekday的值是星期一的1

Ash*_*wal 2 objective-c ios

鉴于NSDate,我试图解析一周中的那一天.

NSDateComponents *comps = [[NSDateComponents alloc] init];
comps=[calendar components: NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitWeekOfMonth | NSCalendarUnitWeekOfYear | NSCalendarUnitWeekday fromDate:date]; 
Run Code Online (Sandbox Code Playgroud)

当date的值为2015-11-30 00:58:52 +0000(这是星期一)时,值comps.weekday打印为1

当date的值为2015-11-15 09:59:46 +0000(这是星期日)时,值comps.weekday也是1.

Tim*_*Tim 5

NSDateComponents使用时区NSCalendar来解释当天.NSCalendar默认为使用设备的时区.

2015-11-30 00:58:52 UTC如果您在UTC时区或其中一个时区,则只是星期一.如果你在EST(-5)时区,那么这将是一个星期天.

如果要在UTC时区解释星期几,请将timeZone属性更改为[NSTimezone timeZoneForSecondsFromGMT:0].

文档编号:https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateComponents_Class/#//apple_ref/occ/instp/NSDateComponents/timeZone