James Dunay
James Dunay

Reputation: 2724

NSCalendar to Display Weekday

Im using an NSCalendar to display the week day, I have no problem displaying the weekday in an integer, but i am confused as to how i might get it to tell me the weekday, i.e. "Thursday." I read that %A, and %a might do the trick but I do not know how to implement them. My code is below.

NSDate* now = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents =[gregorian components:NSWeekdayCalendarUnit fromDate:now];
NSInteger weekday = [weekdayComponents weekday];
myLabel.text = [NSString stringWithFormat:@"%02d", weekday];

I use myLabel to display the string on the Iphone, but if there is a better way please let me know.

Thanks for the help.

James

Upvotes: 0

Views: 2226

Answers (1)

Vladimir
Vladimir

Reputation: 170839

To display day of week you actually need to use NSDateFormatter class, set 'EEEE' format to get full day name (like Thursday) or 'EEE' - for shortened (like Thu). Very sample code:

NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:@"EEEE"];
NSLog([f stringFromDate:[NSDate date]]);

Upvotes: 3

Related Questions