NSDateFormatter,我是在做错什么还是这是一个错误?
时间:2020-03-06 14:49:10 来源:igfitidea点击:
我正在尝试以某种格式打印日期:
NSDate *today = [[NSDate alloc] init]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyyMMddHHmmss"]; NSString *dateStr = [dateFormatter stringFromDate:today];
如果将iPhone设置为24小时制,则可以正常工作;另一方面,如果用户将其设置为24小时制,则可以恢复为AM / PM(在切换此设置之前可以正常工作),然后添加AM / PM最后,即使我没有要求它:
20080927030337 PM
我是在做错什么还是固件2.1的错误?
编辑1:使描述更清晰
编辑2解决方法:事实证明这是一个错误,要解决此问题,我将AM和PM字符设置为"":
[dateFormatter setAMSymbol:@""]; [dateFormatter setPMSymbol:@""];
解决方案
使用我们在模拟器和手机上发布的代码,将2.1固件和24小时制的时间设置为关闭,在执行以下操作时,我从未在dateStr上添加AM / PM:
NSLog(@"%@", dateStr);
我们是否还在使用dateStr进行其他操作,而这些操作都没有在此处发布?我们如何检查值?
跟进
Try turning the am/pm setting on then off. I didn't have the problem either, until I did that. I am printing it out the same way you are.
好的,我也在执行此操作时看到了它。这一定是个错误。我建议我们提交一个错误报告,同时检查并过滤掉不需要的字符。
这是对iPhone SDK错误的解释(在3.1 beta SDK中也存在)
First, a little background on the iPhone user interface. When iPhone users change their region format between, say, “United States” and “France”, the users’ “24-Hour Time” setting is automatically switched to the mode that is most prevalent in that region. In France, that would set 24-Hour Time to “ON”, and in the U.S., that would set it to “OFF”. The users can then manually override that setting and that’s where trouble starts. The problem comes from NSDateFormatter somehow “getting stuck” in the 12 or 24-hour time mode that the user has manually selected. So if a French user manually selects 12-hour mode, and the application requested NSDateFormatter to output time with the 24-hour format “HHmm”, it would actually receive time in a 12-hour format, e.g. “01:00 PM”, as if the application had instead requested “hhmm aa”. The reverse would happen if a US user manually selected 24-hour mode: outputting time with the 12-hour format “hhmm aa” would actually get you time in the 24-hour format instead, e.g. “17:00″.
可以在此博客上找到更多详细信息和可能的解决方法。
对于那些想要使用NSDateFormatter解析24小时时间并遇到此bug的人,使用NSDateComponents解析具有已知格式的日期和时间可以避免此问题:
NSString *dateStr = @"2010-07-05"; NSString *timeStr = @"13:30"; NSDateComponents *components = [[NSDateComponents alloc] init]; components.year = [[dateStr substringToIndex:4] intValue]; components.month = [[dateStr substringWithRange:NSMakeRange(5, 2)] intValue]; components.day = [[dateStr substringFromIndex:8] intValue]; components.hour = [[timeStr substringToIndex:2] intValue]; components.minute = [[timeStr substringFromIndex:3] intValue]; NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDate *date = [calendar dateFromComponents:components]; [components release]; [calendar release];
我认为这是解决方案。
NSDateFormatter *df =[[NSDateFormatter alloc] init]; [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; [df setLocale: usLocale]; [usLocale release]; NSDate *documento_en_Linea =[[[NSDate alloc] init]autorelease]; documento_en_Linea=[df dateFromString:@"2010-07-16 21:40:33"]; [df release]; NSLog(@"fdocumentoenLineaUTC:%@!",documento_en_Linea); //ouput fdocumentoenLineaUTC:2010-07-16 09:40:33 p.m. -0500!
这也应该起作用(尽管我看到了一些奇怪的结果)。
-(NSString*)lowLevTime:(NSString*)stringFormat { char buffer[50]; const char *format = [stringFormat UTF8String]; time_t rawtime; struct tm * timeinfo; time(&rawtime); timeinfo = localtime(&rawtime); strftime(buffer, sizeof(buffer), format, timeinfo); return [NSString stringWithCString:buffer encoding:NSASCIIStringEncoding]; }