0

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?

user2053760
  • 1,673
  • 2
  • 14
  • 19

2 Answers2

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...

https://stackoverflow.com/a/4269211

Community
  • 1
  • 1
Jim
  • 6,753
  • 12
  • 44
  • 72
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