views:

285

answers:

2

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

A: 

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];
Mark
A: 

If you're looking to get the numerical weekday from an NSDate object, you need to use NSCalendar and NSDateComponents like this:

NSDate *date = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:date];
int weekday = [weekdayComponents weekday];
Can Berk Güder