NSDateFormatter issue with date format in iOS 8

2019-02-18 20:57发布

问题:

I have an NSDateFormatter which I use to format an NSDate into a string. The following format does not work: ddMMyyyy_hhmmss_SSS. When I try to format an NSDate I get the following output: 18092014_08:49:03,638, which has a colon and a comma I didn't specify in the format. Note that in iOS 7 I never had this kind of problem. This is the code I'm using:

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat: @"ddMMyyyy_hhmmss_SSS"];
NSString *dateString = [dateFormat stringFromDate:exportDate];

回答1:

iOS8 have serious problem with time format imo. I've just resolved a bug on my application (that i've never had with iOS7) going to settings -> general -> date time -> time format, switching it from 24 to 12 and then back to 24 and all started working again.

Try to make my same procedure, maybe it work also for you!



回答2:

I got problems with NSDateFormatter in iOS 8 too, and had to build a fix version for an app. You could read this. It basically takes to overwrite the default locale:

formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];

But it's best not to use it for formats you want to show to the user. Hope it helps!