For example, given a date 13/09/2013(it is Saturday), how can I know what day it is in code? are there some methods in NSCalendar or NSdate can calculate this for me?
Asked
Active
Viewed 120 times
0
-
Don't know if this is the proper one, if you use NSDateFormatter, with format EEEE you can get the day of the date. – Anupdas Sep 12 '13 at 16:04
-
1BTW: 13-Sep-2013 is a Friday – James Webster Sep 12 '13 at 16:17
-
@JamesWebster maybe it's Saturday in *some* calendar ;) – David Rönnqvist Sep 12 '13 at 16:18
-
I added Sep instead of 9 to counter that :P I'm fairly confident other calendars will have a ninth month, but I doubt they have a "September" =] – James Webster Sep 12 '13 at 16:22
-
[Julian calendar](http://en.wikipedia.org/wiki/Julian_calendar) has September :) – David Rönnqvist Sep 12 '13 at 16:23
-
Phew! 13-Sep-2013 Julian was a Thursday =] – James Webster Sep 16 '13 at 11:21
2 Answers
3
You can use a DateFormatter to get the day of the week for a specific date as a string. You can also use NSDateComponents to get an integer value that represents the day of week.
See this SO post and answers...
1
You can also (but I'm not sure if it's better in any way) get the weekday component and names from the calendar.
NSDate *yourDate = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *weekdayComponent =
[calendar components:NSWeekdayCalendarUnit
fromDate:yourDate];
NSString *dayName = calendar.weekdaySymbols[weekdayComponent.weekday-1];
NSLog(@"today is %@", dayName); // Output: today is Thursday

danielbeard
- 9,120
- 3
- 44
- 58

David Rönnqvist
- 56,267
- 18
- 167
- 205