views:

20

answers:

1

Could anyone explain this to me? I've seen quite a few DateFormatter issues around, and all seem to be related, but not exactly what I'm facing here.

Consider the following code:

NSString * CleanDate = @"20101117T020000-0000";
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyyMMdd'T'HHmmssZ"];  
NSDate * date = [dateFormatter dateFromString:cleanDate];

NSLog(@"CleanDate: %@, Date: %@",cleanDate, date );

The Log output on an iPhone Simulator is

CleanDate: 20101117T020000-0000, Date: (null)

while on the iPad Simulator it is

CleanDate: 20101117T020000-0000, Date: 2010-11-17 03:00:00 +0100

Same results on real devices. Interestingly, everything was working fine in the iPhone Simulator last night. I have reset the simulators to their defaults, changed time zones and languages, without any result.

Should I set something in my Project settings?

+2  A: 

Check the OS versions you're using. There were some changes made to NSDateFormatter in iOS 4. Specifically, it used to discard any text in the string after the date, and return the date. Now, if there's any garbage in there, it return nil. Since the iPad sim is running 3.2, and presumably your iPhone sim is running 4.0, this is most likely the problem.

Ben Gottlieb
Thanks! There happened to be "\r\n" in the end of the string. So, actually the Log output was spread over two lines. I'm very grateful. It's working again. Still a bit confused why last night it was fine. Maybe I was running the simulator on a different OS.
Sjakelien