12

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

Thank you.

Quinn Taylor
  • 44,553
  • 16
  • 113
  • 131
  • Please try to distinguish the difference between Objective-C and Cocoa. It is perfectly feasible to use one without the other, but in the case of dates I assume you are talking about Cocoa. – dreamlax Jun 29 '09 at 09:55
  • While they are distinct entities, it's arguably easier to use Objective-C without Cocoa than vice-versa. Even if they were to use Cocoa from Python or whatever, the classes and method calls would be exactly the same. Also, the "objective-c" tag is not near as active as Java or C#, and pretty much everyone who is viewing this tag is interested in Mac or iPhone programming. – Quinn Taylor Jun 29 '09 at 14:10
  • Did you read the unedited question? It did not make any reference to Cocoa, hence the edit and comment. I think it is important to make the distinction, just as people should with C++ and STL, C# and .NET, etc. – dreamlax Jun 29 '09 at 20:06
  • 1
    I'm not sure if I saw it before it was edited, (is there a way to view revisions of a question?), and I can understand that the answer to his question is Cocoa-specific. However, I'd guess that > 95% of Cocoa devs are using Objective-C, and > 99% of all Objective-C devs are using Cocoa (as opposed to GNUstep, etc.). The two tags are very closely related, and I didn't figure that tagging with Objective-C would anger fans of the language that don't use Cocoa. If I'm totally wrong on that count, I have no problem with removing the Objective-C tag. :-) – Quinn Taylor Jun 29 '09 at 21:59
  • It was the title of the question, not the tags. The objective-c and cocoa tags are very fitting for the question, but before, without any reference to Cocoa, I could have provided a POSIX solution and it would have fit the criteria of the question. Most Cocoa developers are using Objective-C and most Objective-C developers are using Cocoa but that's no reason use the two terms interchangeably. – dreamlax Jun 29 '09 at 22:38

4 Answers4

28

I've been doing:

NSDateFormatter* theDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[theDateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[theDateFormatter setDateFormat:@"EEEE"];
NSString *weekDay =  [theDateFormatter stringFromDate:[NSDate date]];

This has the added bonus of letting you choose how you'd like the weekday to be returned (by changing the date format string in the setDateFormat: call

Lots more information at:

http://developer.apple.com/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369

Jeff Hellman
  • 2,117
  • 18
  • 17
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];
ThibThib
  • 8,010
  • 3
  • 30
  • 37
6

Use NSCalendar and NSDateComponents. As shown in the NSDateComponents documentation:

NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:comps];
NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:date];
int weekday = [weekdayComponents weekday];
Chuck
  • 234,037
  • 30
  • 302
  • 389
  • 1
    Where is "comps" coming from? Also, technically @ThisThib is correct that -weekday returns an NSInteger value. Shouldn't be a problem for any calendar I know, but in the name of correctness and portability... ;-) – Quinn Taylor Jun 29 '09 at 14:14
-1

Here's what I wound up with, which works exactly as intended. It takes a date, alters the date to be the first of the month, and gets the day of that date:

    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:date];
NSDateComponents *monthDateComponents = [[NSCalendar currentCalendar] components: NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear |
                                         NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate: date];

//  build date as start of month
monthDateComponents.year = components.year;
monthDateComponents.month = components.month;
monthDateComponents.day = 1;

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
//    [gregorian setFirstWeekday:1];

NSDate *builtDate =[gregorian dateFromComponents: monthDateComponents];
//    NSDateComponents *weekdayComponents =[gregorian components: NSCalendarUnitWeekday fromDate: builtDate];

NSDateFormatter *df = [NSDateFormatter new];
[df setDateFormat:@"E"];

NSString *firstDay = [df stringFromDate:builtDate];
if([firstDay isEqual:@"Sun"])  //  do for the other 6 days as appropriate
    return 7;
SpokaneDude
  • 4,856
  • 13
  • 64
  • 120