Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top