2012-08-06 68 views
0

我想得到星期幾,最好是從1到7的整數......這可能嗎?如何使用NSCalendar獲得星期幾?

這是我目前要得到當前的小時,分​​鍾和秒....

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

您可以使用此:

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

希望有所幫助。