2008-09-27 158 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]; 
相關問題