2011-05-29 161 views
15

我想輸出日期(具有相對日期,例如Today,Yesterday等)和時間(尊重12/24小時在iOS設備上設置)例如NSDateFormatter - 在某些情況下不尊重12/24小時(am/pm)系統設置

  • 今天下午5:48(在12小時模式時);和
  • 今天17:48時(24小時模式)

配置NSFormatter像這樣(使用stringFromDate :)不起作用:

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setLocale: [NSLocale autoupdatingCurrentLocale]]; 
[dateFormatter setTimeStyle:NSDateFormatterShortStyle]; // doesn't respect am/pm setting on iOS device (grrr!) 
[dateFormatter setDateStyle:NSDateFormatterMediumStyle]; 
[dateFormatter setDoesRelativeDateFormatting:YES]; // but does get relative dates 

這給你相對日期,但默認爲語言環境的12/24設置,例如美國12小時,英國24小時。 (爲什麼蘋果認爲英國人喜歡24小時制鐘表,我不知道...)

我所採用的解決方案使用了兩個NSDateFormatters,一個用於日期和一個時間。看起來,當您使用NSDateFormatterNoStyle的dateStyle和NSDateFormatterShortStyle的timeStyle配置NSDateFormatter時,它確實遵守12/24小時設置。 (所以這只是當你有兩個日期和時間樣式設置,有麻煩。)

我已經包括我的工作,以防其他人有類似的問題。有沒有更簡單的方法來做到這一點?這個工作似乎有些尷尬,不清楚我應該如何確信它將在iOS的未來版本中繼續工作。

解決方法

- (NSString*) humanRelativeDateAndTimeAlt: (NSDate*) date; 
{ 

NSDateFormatter* relativeDateFormatter = [[NSDateFormatter alloc] init]; 
[relativeDateFormatter setLocale: [NSLocale autoupdatingCurrentLocale]]; 
[relativeDateFormatter setTimeStyle:NSDateFormatterNoStyle]; // no time for this formatter 
[relativeDateFormatter setDateStyle:NSDateFormatterMediumStyle]; 
[dateFormatter setDoesRelativeDateFormatting:YES]; // relative dates 

NSString* datePart = [[self relativeDateFormatter] stringFromDate:date]; 

NSDateFormatter* timeDateFormatter = [[NSDateFormatter alloc] init]; 
[timeDateFormatter setLocale:[NSLocale autoupdatingCurrentLocale]]; 
[timeDateFormatter setDateStyle: NSDateFormatterNoStyle]; // no date for this formatter 
[timeDateFormatter setTimeStyle: NSDateFormatterShortStyle]; // does respect 12/24 hour setting 

NSString* timePart = [timeDateFormatter stringFromDate:date]; 

int skipZero = 0; // extra code to remove leading zero if present 
if ([[timePart substringToIndex:1] isEqualToString:@"0"]) skipZero = 1; 

[relativeDateFormater release]; 
[timeDateFormatter release]; 

return [NSString stringWithFormat:@"%@ %@", datePart, [timePart substringFromIndex:skipZero]]; 
} 

回答

4

我遇到了同樣的問題。我提出它與蘋果的錯誤,並張貼在打開雷達:

http://openradar.appspot.com/9774877

+0

我確認了這個錯誤。它仍然存在於iOS 7中。在另一個線程上有關於此的廣泛討論:http://stackoverflow.com/questions/143075/nsdateformatter-am-i-doing-something-wrong-or-is-this-a-然而,錯誤 – 2013-10-01 06:36:34

1

我有一個類似的問題,我落得這樣做是爲了ammend的日期格式,而不是指定timeStyle。

NSDateFormatter *formatter = [[ NSDateFormatter alloc ] init]; 

    if (self.datePickerMode == UIDatePickerModeDateAndTime){ 
     [formatter setDateFormat:[NSString stringWithFormat:@"%@ %@", f.dateFormat, @"HH:mm:ss"]]; 
    } 

對我很好,希望對你有幫助。

12

這樣做的原因行爲是語言環境,正確的區域設置,設置本地的NSDateFormatter的設置爲en_US_POSIX將解決這個問題。 它適用於24小時制和12小時制。

Apple doc

在iPhone OS,而用戶可以更改默認的AM/PM與24小時的時間設置(通過設置>通用>日期&時間> 24小時時間),這將導致NSDateFormatter重寫您設置的格式字符串。

參考:What is the best way to deal with the NSDateFormatter locale 「feature」?

3

我發現此解決方案:

NSDate *now = [[NSDate alloc] init]; 
NSTimeZone *localTimeZone = [NSTimeZone systemTimeZone]; 
NSDateFormatter *rfc3339DateFormatter = [[NSDateFormatter alloc] init]; 
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]; 

[rfc3339DateFormatter setLocale:enUSPOSIXLocale]; 
[rfc3339DateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ssZ"]; 
[rfc3339DateFormatter setTimeZone:localTimeZone]; 

NSString *dateString = [rfc3339DateFormatter stringFromDate:now]; 

NSLog(@"RFC 3339 datetime: %@", dateString); 
// 2013-06-27T10:27:08-0600 
// 2013-07-22T15:09:30+0100 

即固定的我的問題2:

  1. 當用戶被設置好的至12H顯示模式,在該輸出格式化程序顯示爲24小時
  2. 當您手動設置爲都柏林時區,輸出不以Z顯示日期時間,但+0000
1

使用此格式

NSDateFormatter* relativeDateFormatter = [[NSDateFormatter alloc] init]; 
dateFormatter.dateFormat = @"yyyy-MM-dd hh:mm a"; 

NSString *stringDate = [dateFormatter stringFromDate:[NSDate date]]; 
  1. Instad的HH(一天中的小時(0-23))使用HH(小時am/pm(1-12))
  2. a(Am/pm marker)格式結尾。

例如:[NSDate的日期]爲2017年5月14日12:40:00

  • 在24小時的時間,你會得到2017年5月14日12:40
  • 在12小時的時間,你會得到2017年5月14日12:40 PM

對於今天|昨天我使用NSDateComponents計算並返回正確的日期。

NSCalendarUnit units = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute; 
NSDateComponents *components = [[NSCalendar currentCalendar] components:units fromDate:yourDate toDate:[NSDate date] options:0]; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 

if (components.year > 0) { 
    dateFormatter.dateFormat = [NSString stringWithFormat:@"'%@' '%@' MMM d, YYYY", [@"last seen" localized], [@"at" localized]]; 
} 
else if (components.day > 7) { 
    dateFormatter.dateFormat = [NSString stringWithFormat:@"'%@' MMM d '%@' hh:mm a", [@"last seen" localized], [@"at" localized]]; 
} 

NSString *relativeDateString = [dateFormatter stringFromDate: yourDate]; 
+0

注意到,在現代iOS中,Apple推薦使用日期格式化程序的唯一方法是:https://stackoverflow.com/a/42370648/294884 – Fattie 2017-07-28 13:53:39

相關問題