2008-09-27 159 views
37

我试图打印出某种格式的日期:如果iPhone设置为24小时时间NSDateFormatter,我做错了什么或这是一个错误?

NSDate *today = [[NSDate alloc] init]; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"yyyyMMddHHmmss"]; 
NSString *dateStr = [dateFormatter stringFromDate:today]; 

,这工作正常,如果在另一方面,用户已经将其设置为24小时时间,然后回到AM/PM(它,直到你切换此设置正常工作),那么它附加上月底AM/PM,即使我没有问它:

20080927030337 PM 

难道我做错了什么或者这是固件2.1的错误?

编辑1:制造描述清晰的

编辑2解决方法:事实证明这是一个错误,解决它,我设定的AM和PM字符“”:

[dateFormatter setAMSymbol:@""]; 
[dateFormatter setPMSymbol:@""]; 
+1

准确获取同样的问题 - 高兴地看到,我不是要疯了! – 2009-01-31 22:20:05

+0

请注意,如果您使用setAM/PM方法,字符串中仍然会有额外的空间。 – conor 2014-03-27 12:18:08

回答

12

使用代码你贴在模拟器上,并与设置为关闭2.1固件和24小时的时间电话都,我从未有过的AM/PM追加到dateStr当我做:

NSLog(@"%@", dateStr); 

你在做别的用d ateStr,你没有在这里发布?你如何检查价值?

跟进

尝试了又灭,打开AM/PM设置。我也没有问题,直到我做到了。我用与你一样的方式打印出来。

好的,我也看到它,当我这样做。这是一个错误。我建议你file a bug report,并在此期间检查并过滤掉不需要的字符。

+0

尝试关闭am/pm设置。我也没有问题,直到我做到了。我用与你一样的方式打印出来。 – rustyshelf 2008-09-27 06:51:26

+0

这个答案不是解决方案 - huyz答案正确地回答了这个问题 - 这是一个NSDateFormatter的错误 – earnshavian 2012-06-09 19:21:37

14

下面是iPhone SDK错误的解释(也是在3.1测试版SDK仍然存在)

首先,iPhone的用户界面上的一些背景知识。当iPhone 用户更改之间,说,“美国”和 “法国”的区域格式,用户的‘24小时时间’设置自动切换 到这是最普遍的在该地区的模式。在法国, 会将24小时时间设置为“开”,而在美国,这会将其设置为 “关”。然后用户可以手动覆盖该设置,并且这是故障开始的地方 。

问题来自NSDateFormatter以某种方式在用户手动选择的12或24小时时间模式下“卡住” 。因此,如果一个 法语用户手动选择12小时的模式,并且该应用程序要求 到NSDateFormatter输出时间与24小时格式 “HHMM”,它实际上接收时间在12小时格式,例如 “01:00 PM”,好像应用程序改为请求“hhmm aa”。 如果美国用户手动选择24小时模式,则会发生相反情况: 以12小时格式输出时间“hhmm aa”实际上会以24小时制格式获得 ,例如, “17:00“。

更多详细信息和可能的解决方法可以在blog找到。

+0

提交了一个bug:http://openradar.appspot.com/radar?id=1110403 – 2011-02-11 15:10:11

1

对于那些寻找这个问题谁想要使用NSDateFormatter来分析24小时的时间,都达不到这个错误,使用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]; 
4

我认为这是解决方案。

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! 
0

这也应该工作(虽然我看到一些bizzare结果)。

-(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]; 
} 
9

的日期格式为en_US设置本地化解决了这个问题对我来说:

NSDateFormatter * f = [[NSDateFormatter alloc] init]; 
    [f setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"]; 
    f.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0]; 
    f.calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease]; 
    f.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]; 

我不知道,如果将日历也需要,但这种效果很好。

0

简答题:尝试[dateFormatter setDateFormat:@"yyyyMMddhhmmss"]; 12小时格式(注意小写字母hh)。

这是一个令人沮丧的话题,因为如此多的网站表示使用HH数小时(包括苹果官方文档),但它将其设置为24小时格式,而hh使用12小时格式。有关更多详细信息,请参阅http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns

作为奖励,请注意,您也可以使用KKkk作为一天中的小时格式,该格式可能会被忽略。

更新: 我最近在看NSLocale(https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSLocale_Class/Reference/Reference.html),它会似乎您可以使用autoupdatingCurrentLocale将从应用程序中所做的更改应用于语言环境。这样做的结果是,即使手机设置为使用24小时制(如切换到法国时),也可以为应用程序制作12/24切换,这不会影响手机上的任何其他应用程序,或要求您离开应用程序进行更改。

25

这样做的原因行为是语言环境,正确的区域设置,设置本地的NSDateFormatter的设置为en_US_POSIX将解决这个问题。 它适用于24小时制和12小时制。

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.apple doc

试试这个,

NSDate *today = [[NSDate alloc] init]; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]]; 
[dateFormatter setDateFormat:@"yyyyMMddHHmmss"]; 
NSString *dateStr = [dateFormatter stringFromDate:today]; 
相关问题