2

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.

Duck
  • 34,902
  • 47
  • 248
  • 470

2 Answers2

1

NSData is referenced to a base date "the first instant of 1 January 2001, GMT", it is simply the number of seconds since that base date.

Dates by definition are location sensitive, as I write this my friend in the UK is already in tomorrow.

Day of week is calendar sensitive.

Thus the timezone and calendar need to be specified. In my case they seem to be correct by default but are best specified explicitly.

So, set the timezone:

- (void)setTimeZone:(NSTimeZone *)tz
zaph
  • 111,848
  • 21
  • 189
  • 228
  • I think you are not getting my point. If I ask one of those amazing savant guys what day was December 23, 2001. They will ask "MONDAY", they will not question me "which timezone?". How can a date can be timezone sensitive in this sense. December 23, 2001 will be monday or whatever day it was, in all world, some point in time. December 23, 2001 will not be monday in the west and tuesday in the east. I see that your answer solves the problem. I am just asking and ranting. – Duck Aug 29 '11 at 00:37
  • Apple had to make a choice, base NSDate on GMT or the local timezone. They chose GMT. Now, I find Apple's date/calendar API to be very difficult to use, it seems to me that it could be made much easier to use in the common case, they chose not to do that. I have files a bug concerning the awkwardness of these APIs, please do so as well! – zaph Aug 29 '11 at 00:41
  • I agree. It is so difficult that even they had problems recently with the calendar events not firing on the correct date. A generic date cannot have any timezone associated. A date is a date, in Shanghai or New York. I am working on an app that has events in time. My hair is gray of hate, trying to do simple tasks as comparing dates, calculating future and past dates, etc. It is much more complex than needed. Their dates API sucks infinity squared. I will fill a bug report now. THanks. – Duck Aug 29 '11 at 00:48
  • 1
    Consider watching WWDC11 session 117, "Performing Calendar Calculations", I did, it helped. – zaph Aug 29 '11 at 00:59
0

What's your time zone? Chances are you are one day off because your time zone is sufficiently different from GST that the time you are representing is the next or previous day.

I have found it necessary to specify the date as being in the local time zone to avoid this.

Adam Eberbach
  • 12,309
  • 6
  • 62
  • 114
  • My timezone is GMT, but we are now on summertime, so we are +1. I hate this date stuff of iOS. I am trying to calculate the day of week of a generic date. Why do I have to include the timezone? This is stupid. So, If I want to calculate what day of week was January 1, 1970 I have to include a timezone? The programmer who created this is suffering from a severe mental disease. – Duck Aug 29 '11 at 00:05