8
votes

In my iPhone app, I am trying to get the EEE MMM dd from a date string received by a web service.

For all dates where time is < 07:00, the NSDateFormatter returns a date off by 1 day. For example: Date to format: 2010-11-17T05:00:00, formatted date result: Tue Nov 16

Here's the code snippet: NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat: @"EEE MMM dd"];

NSDate *dateToFormat = [NSDate dateWithNaturalLanguageString:dateStringToFormat locale:[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]];

NSString *formattedDateString = [dateFormat stringFromDate:dateToFormat]; [dateFormat release];

What could be the problem?

2
Where do you live, and which timezone does the web service use?Yuji

2 Answers

10
votes

Probably has something to do with time zones. You probably need to set an appropriate value for the timeZone property of your NSDateFormatter object.

2
votes

I had this same issue but only after updating to iOS4. My formatter is like this:

[aDateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss z"];

and my dates like this:

2009-10-27 7:30:12 -0700

I managed to fix the issue just by moving the date picker mode set before applying the max/min/selected date to the date picker. In my case this:

aDatePicker.datePickerMode = UIDatePickerModeDate;