如何使用NSCalendar获取星期几?

时间:2012-08-06 20:27:22

标签: iphone ios xcode

我想得到一周中的某一天,最好是1到7的整数...这可能吗?

这是我目前获得的当前小时,分钟和秒......

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [gregorian components:(NSHourCalendarUnit  | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:[NSDate date]];

NSInteger hour = [dateComponents hour];
NSInteger minute = [dateComponents minute];
NSInteger second = [dateComponents second];

1 个答案:

答案 0 :(得分:1)

您可以使用:

NSDateComponents *weekdayComponents =[gregorian components:NSWeekdayCalendarUnit fromDate:myDate];

NSInteger weekday = [weekdayComponents weekday];

或者与您已有的代码集成:

NSDateComponents *dateComponents = [gregorian components:(NSHourCalendarUnit  | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate:[NSDate date]];

NSInteger weekday = [dateComponents weekday];

这将使你获得值1 .. 7,其中1是星期日。

希望有所帮助。