views:

283

answers:

3

Hi,

Im building a calendar and to find out the first day of the month I do

[formatter setDateFormat:@"e"];
int startDay = [[formatter stringFromDate:newDate] intValue];

On the device this works correctly and the 1st of the month is on the correct day. But on the simulator it is the day after. Although it doesn't overly matter about the simulator it is kind of driving me crazy thinking I've done something wrong. I tried to set the locale of the formatter but no difference, nor i think should it. Can anyone shed some light on this?

On a side note is there a better way to build a calendar than this?

Update: It seems if I dont set the locale it works fine on device but on the simulator it doesn't. If I do set the locale to en_US neither of them work. I'm in Aust if that changes anything but I'm pretty sure both the US and Aust have the same calendar :/

A: 

That sounds like correct behavior depending on the date you're passing in. An NSDate corresponds to a precise point in universal time. At that point, it may be Monday in one time zone and Tuesday in another.

You probably want [[NSCalendar currentCalendar] firstWeekDay].

Tom
Hmm, thought I read NSCalendar wasn't available on the iPhone but now I look into it it is. I'll have a look
Rudiger
[[NSCalendar currentCalendar] firstWeekDay] returns the first day of the week, ie Monday. You can also set it so it would be a sunday too. Either way it just returns 1 for every month. Still looking into the NSDate being passed. Im not setting a time or timezone, just a date, so not sure exactly what is happening.
Rudiger
A: 

There doesn't seem to be a solution to this. Changing the timezone on the device doesn't cause an issue and all log output reports the proper timezone but NSDate seems to pull the timezone from two different locations on the simulator, probably one for Cupertino and one for local.

Looking at other peoples solution to getting the first day of the month it is the same as my code (odd because I didn't copy) and no one has reported a problem, perhaps those users were in the U.S and so the problem didn't surface. Changing the timezone on the computer doesn't do anything either.

Rudiger
A: 

What if you have different Calendars on Emulator and Device? One has Monday as a firstWeekDay and another has Sunday. Probably this could be a reason of the problem...

Denis Khitrov
The phone syncs its date from the computer and I have never had a problem with other sections of the phone in say calendar.
Rudiger