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];
How to get day of week from NSDate?
Question marked as
Best reply
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];
9 replies
Question marked as
Best reply
something like this:
I entered "day of week" into the Xcode's Documentation search box and the first hit looks like it might be useful: the Calendrical Calculations article in Date and Time Programming Guide for Cocoa.
Here's a way to get the name of the weekday as an NSString if you've already got an NSDate...
The output is something like:
You can use a lower-case "%a" as the descriptionWithCalendarFormat if you just want the three character weekday abbreviation ("Mon", "Tue", "Wed", etc).
Steve
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
If you're developing for the iPhone, NSDate's descriptionWithCalendarFormat isn't available. (for some reason..)
For the iphone this code will work:
- (NSString *)getDayOfTheWeek:(NSDate *)date{
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc]
initWithDateFormat:@"%A" allowNaturalLanguage:NO] autorelease];
NSString *formattedDateString = [dateFormatter stringFromDate:date];
return formattedDateString;
}
How about this:
NSString *myString=[myDate descriptionWithCalendarFormat:"%w" timezone:nil locale:nil] ;
That should return the string "0" for Sunday, "1" for Monday, etc.
NSString *myString=[myDate descriptionWithCalendarFormat:"%w" timezone:nil locale:nil] ;
That should return the string "0" for Sunday, "1" for Monday, etc.
Let's try that again! Formatted correctly this time:
How about this:
NSString *myString=\[myDate descriptionWithCalendarFormat:"%w" timezone:nil locale:nil\] ;
That should return the string "0" for Sunday, "1" for Monday, etc.
How about this:
NSString *myString=\[myDate descriptionWithCalendarFormat:"%w" timezone:nil locale:nil\] ;
That should return the string "0" for Sunday, "1" for Monday, etc.
I like this approach. I've been looking for something like this to use in one of my applications, and I'm surprised I didn't think of this before.
I forgot the at-sign and misspelled timeZone -- here is the corrected version:
NSString *myString=\[myDate descriptionWithCalendarFormat:@"%w" timeZone:nil locale:nil\] ;
Message was edited by: DarelRex
NSString *myString=\[myDate descriptionWithCalendarFormat:@"%w" timeZone:nil locale:nil\] ;
Message was edited by: DarelRex
How to get day of week from NSDate?