How to find weekday from today's date using NSDate?
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.
If someone is interested in a Swift solution, then use:
import Foundation
let weekday = NSCalendar.current.component(.weekday, from: Date())
print(weekday) // => prints the weekday number 1-7 (Sunday through Saturday)
See the demo.
Update: fixed to work in Swift 3