开发者

NSDateformatter give 'null' on iPhone, correct date on iPad

开发者 https://www.devze.com 2023-01-20 22:46 出处:网络
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.

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 setDat开发者_如何转开发eFormat:@"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?


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.

0

精彩评论

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