0

我想得到星期几,最好是从 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];
4

1 回答 1

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 是星期日。

希望有帮助。

于 2012-08-06T20:32:00.313 回答