iPhone - Calculating the Day of Week of any date
<p>I have a NSDate variable "aDate" that represents a date, for example, sunday, August 28, 2011. I have this date on a NSDate variable. This can be any date, but the problem appears when the day is a sunday.</p>

<p>I want to obtain the three letter string representing the day of week. In this case, SUN.</p>

<p>Then I have this code:</p>

<pre><code>NSDateFormatter* theDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[theDateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[theDateFormatter setDateFormat:@"EEE"];  
[theDateFormatter setLocale:currentLocale]; // I have tried to comment this out... no change

NSString *dayOfWeek =  [theDateFormatter stringFromDate:aDate];
</code></pre>

<p>the result I have is MON.  Monday?</p>

<p>The insanity just goes wilder if I add this code:</p>

<pre><code>NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *comps = [gregorian components:NSWeekdayCalendarUnit fromDate:myDate];
int weekday = [comps weekday];
</code></pre>

<p>then weekday comes as 2 (MONDAY ??????) WTF? Confirming the problem.</p>

<p>I am on a locale that in theory monday is the first day of week. So, independently of my timezone any date just have one day of week. If I want to calculate what day of week was January 1, 1978, I shouldn't need to define any timezone.</p>

<p>How do I do that on iPhone?</p>

<p>thanks.</p>
