问题
NSDate *currentDate = [NSDate date];
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"MM/dd/yyyy,h,mm,a"];
NSString *dateString = [format stringFromDate:currentDate];
[format release];
NSLog(dateString);
Output in: Device iPhone4
[240:707] 10/25/2011,22,23,
Current language: auto; currently objective-c
warning: Unable to read symbols for /SDK4.2/Platforms/iPhoneOS.platform/DeviceSupport/4.3.3 (8J2)/Symbols/Developer/usr/lib/libXcodeDebuggerSupport.dylib
Output in: Device iPad2
[82:707] 10/25/2011,12,28,PM
Current language: auto; currently objective-c
warning: Unable to read symbols for /SDK4.2/Platforms/iPhoneOS.platform/DeviceSupport/4.3.3 (8J2)/Symbols/Developer/usr/lib/libXcodeDebuggerSupport.dylib
This thing works fine on Simulator for all.
How to handle this?
More Info:
it is also to notice that the same formatting string resulting in 12 hr and 24 hr clock values in return
回答1:
Sounds like you have a different locale set on your iPad and your iPhone, see here:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW1
Either set the same locale on both devices (if it's just your problem) or make sure the NSDateFormatter uses the same (hard-coded) locale if you rely on the format being like a certain format.
Edit: Additional Info
It apparently IS a problem with the locale, more detailed here (with solutions): http://developer.apple.com/library/ios/#qa/qa1480/_index.html
But I was wrong, setting the locale will not help it indeed.
Citation:
On iPhone OS, the user can override the default AM/PM versus 24-hour time setting (via Settings > General > Date & Time > 24-Hour Time), which causes NSDateFormatter to rewrite the format string you set, which can cause your time parsing to fail.
回答2:
finally resolved it. it was due to the settings in phone as 24 Hr clock.
I had to update my code accordingly.
:)
来源:https://stackoverflow.com/questions/7885742/date-formatter-different-output-on-different-devices-running-same-ios-version