Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I know that I can figure out today's date by [NSDate date]; but how would I find out today day of the week, like Saturday, Friday etc.

I know that %w can be used in NSDateFormatter, but I don't know to use it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
469 views
Welcome To Ask or Share your Answers For Others

1 Answer

NSCalendar* cal = [NSCalendar currentCalendar];
NSDateComponents* comp = [cal components:NSCalendarUnitWeekday fromDate:[NSDate date]];
return [comp weekday]; // 1 = Sunday, 2 = Monday, etc.

See @HuguesBR's answer if you just need the weekday without other components (requires iOS 8+).

NSInteger weekday = [[NSCalendar currentCalendar] component:NSCalendarUnitWeekday 
                                                   fromDate:[NSDate date]];

(If you don't get a correct answer, check if you have mistyped NSCalendarUnitWeekday with other week-related components like NSCalendarUnitWeekdayOrdinal, etc.)


Swift 3:

let weekday = Calendar.current.component(.weekday, from: Date())
// 1 = Sunday, 2 = Monday, etc.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...