9

Possible Duplicate:
Objective C - How can i get the weekday from NSDate?

How do I get the day of the week using NSDate and show using NSLog?

I wonder the current day, such as today.

Ex: NSLog(@"the day is:",day)
Resp: "the day is: Monday"

Community
  • 1
  • 1
imalve
  • 153
  • 2
  • 2
  • 6

3 Answers3

23
NSDateFormatter* day = [[NSDateFormatter alloc] init];
[day setDateFormat: @"EEEE"];
NSLog(@"the day is: %@", [day stringFromDate:[NSDate date]]);
tronbabylove
  • 1,102
  • 8
  • 12
16

Using NSDateComponents:

static inline NSString *stringFromWeekday(int weekday)
{
    static NSString *strings[] = {
        @"Sunday",
        @"Monday",
        @"Tuesday", 
        @"Wednesday",
        @"Thursday",
        @"Friday",
        @"Saturday",
    };

    return strings[weekday - 1];
}


NSDateComponents *components = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:[NSDate date]];

NSLog(@"%@", stringFromWeekday([components weekday]));

Of course, this doesn't respect locales & such.

Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
  • 5
    Don't hardcode the weekday names. You can get them from `NSDateFormatter weekdaySymbols`. – rmaddy May 20 '15 at 21:51
0
NSLog(@"the day is: %@", [[NSDate date] descriptionWithCalendarFormat:@"%A" timeZone:nil locale:nil]);
michex
  • 112
  • 3