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.
I want to obtain the three letter string representing the day of week. In this case, SUN.
Then I have this 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];
the result I have is MON. Monday?
The insanity just goes wilder if I add this code:
NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *comps = [gregorian components:NSWeekdayCalendarUnit fromDate:myDate];
int weekday = [comps weekday];
then weekday comes as 2 (MONDAY ??????) WTF? Confirming the problem.
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.
How do I do that on iPhone?
thanks.