Search code examples
iphonecalendardayofweek

Need a list of week days with their number on iphone os


I'm working on a project that needs to have a list of weekdays.

I could get their locale names using the NSDateFormatter without a problem, but I was hoping to have an integer weekday also to save on the database and do some work.

Where can i get that number?

Thanks, Leonardo


Solution

  • See Apple Developers thread on NSDateComponents and weekday

    NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
    
    NSDateComponents *weekdayComponents =
            [gregorian components:NSWeekdayCalendarUnit fromDate:dateOfInterest];
    
    NSInteger weekday = [weekdayComponents weekday];
    // weekday 1 = Sunday for Gregorian calendar
    
    [gregorian release];