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

Edit for Mac:

The format of the string has to be —YYYY-MM-DD HH:MM:SS ±HHMM according to the docs, all fields mandatory

Old answer (for iPhone and tested on simulator):

There is no (public) -initWithString: method in NSDate, and what you get returned is not what you expect.

Use a properly configured (you need to give the input format) NSDateFormatter and -dateFromString:.


Creating an NSDateFormatter that only contains the weekday will do what you want.

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

If you need internationalization, the NSDateFormatter can be sent a locale, which will give the proper translation.

The date formatters are controlled through this standard: Unicode Date Formats