How to get day of week from NSDate?

How to get day of week from NSDate?

Posted on Sep 8, 2008 5:08 PM

Reply
9 replies

Sep 8, 2008 8:19 PM in response to aapl.crox

something like this:


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];

Sep 8, 2008 9:41 PM in response to aapl.crox

Here's a way to get the name of the weekday as an NSString if you've already got an NSDate...

NSDate * testDate = [NSDate date];
NSString * weekdayString = [testDate descriptionWithCalendarFormat:@"%A" timeZone:nil
locale:[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]];
NSLog(@"Day of the week: %@", weekdayString);

The output is something like:

Day of the week: Monday

You can use a lower-case "%a" as the descriptionWithCalendarFormat if you just want the three character weekday abbreviation ("Mon", "Tue", "Wed", etc).

Steve

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How to get day of week from NSDate?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.