3

Possible Duplicate:
how do I get the day of the week using NSDate and show using NSLog in iOS?

Using the following code.

NSDateFormatter* day = [[NSDateFormatter alloc] init];  
[day setDateFormat: @"EEEE"];  
NSLog(@"the day is: %@", [day stringFromDate:[NSDate date]]);

how do I put this value in a variable?
I want to use an if, for example:

if (day == "monday")  
{  
  ...    
}
Community
  • 1
  • 1
imalve
  • 153
  • 2
  • 2
  • 6
  • 3
    Double-post of [how do I get the day of the week using NSDate and show using NSLog in iOS?](http://stackoverflow.com/questions/9874503/how-do-i-get-the-day-of-the-week-using-nsdate-and-show-using-nslog-in-ios) from three hours ago. Please don't repost questions here. – jscs Mar 26 '12 at 18:50

1 Answers1

15

This is how you do it. Always set the locale in which you'd like to get the day name, because the default one may not be English.

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
dateFormatter.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];
[dateFormatter setDateFormat:@"EEEE"];
NSString *dayName = [dateFormatter stringFromDate:[NSDate date]];
if ([dayName isEqualToString:@"Monday"]) {
}
Eugene
  • 10,006
  • 4
  • 37
  • 55