开发者

NSDateFormatter - day of week different on simulator vs hardware

I'm trying to determine the current day of the week in my app. To do this I have:

NSDateFormatter* dayDateFormatter = [[NSDateFormatter alloc] init];
[dayDateFormatter setDateFormat:@"e"];
int todayDayNum = [[dayDateFormatter stringFromDate:[NSDate date]] intValue];
[dayDateFormatter release];
NSLog(@"%d", todayDayNum);

Today is currently a Thursday for me, and on my iPhone I am getting the value 4 as the result. However, on the simulator I get 5.

Is there something I am doing wrong? I have tried both c and e as the format string. I have also double checked that the date/time is correct (and the same) on both the simulator and the device.

Edit: I've changed my code to the following, which seems to work consistently now (I get 5 for both my device and the simulator. I am guessing that this is becaus开发者_如何学Pythone my device is using some other calendar?

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents = [gregorian components:(NSWeekdayCalendarUnit) fromDate:[NSDate date]];
NSInteger todayDayNum = [weekdayComponents weekday];
NSLog(@"%d", todayDayNum);


try to play with [gregorian setFirstWeekday:1]; // Sunday == 1, Saturday == 7 etc

or locale - [gregorian setLocale:[NSLocale currentLocale]]; because it influenced too

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜