2

Possible Duplicate:
How do I get the day of the week in Objective-C?

How do I print the day of the week? What object should I use? I need the day (e.g. Sunday, Thursday), not the date in the month.

Update: Specifically: how can I find the number of the day of the week (e.g. 1, 5)

Community
  • 1
  • 1
Lucy Weatherford
  • 5,452
  • 16
  • 50
  • 76
  • I'm actually looking for the *number* of the day of the week – Lucy Weatherford Jul 07 '12 at 18:48
  • Then _why_ did you say "e.g. Sunday, Thursday"? How to get the index is pretty easy to figure out from [the docs](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DatesAndTimes/DatesAndTimes.html), and [this SO answer](http://stackoverflow.com/a/1057405/603977) also tells you how. – jscs Jul 07 '12 at 19:16

1 Answers1

8

Make an instance of NSDate (for right now, use [NSDate date]), and then use an NSDateFormatter and a format string as described here.

NSDate *today = [NSDate date];
NSDateFormatter *myFormatter = [[NSDateFormatter alloc] init];
[myFormatter setDateFormat:@"EEEE"]; // day, like "Saturday"
[myFormatter setDateFormat:@"c"]; // day number, like 7 for saturday

NSString *dayOfWeek = [myFormatter stringFromDate:today];
NSLog(@"Today is: %@", dayOfWeek);
Chris Trahey
  • 18,202
  • 1
  • 42
  • 55