Alex Rivera | Logout

How to find what day of the week for any given date using Cocoa

Asked 2009-06-29T09:15:01.687
12

I'm trying to figure out what day (i.e. Monday, Friday...) of any given date (i.e. Jun 27th, 2009)

Thank you.

Edit
Report

1 Answer

16

You may have a look to the NSDate and NSCalendar classes. For example, here and here

They provide the following code:

NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc]
                         initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents =
                [gregorian components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:today];
NSInteger day = [weekdayComponents day];
NSInteger weekday = [weekdayComponents weekday];
answered 2009-06-29T09:30:05.043

Your Answer