【问题标题】:NSDateFormatter - does not respect 12/24 hour (am/pm) system setting in some circumstancesNSDateFormatter - 在某些情况下不遵守 12/24 小时(上午/下午)系统设置
【发布时间】:2011-09-04 08:58:05
【问题描述】:

我想输出一个日期(具有相对日期,例如今天、昨天等)和时间(尊重 iOS 设备上的 12/24 小时设置)例如

  • 今天下午 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 小时。 (为什么 Apple 认为英国人喜欢 24 小时制,我不知道......)

我采用的解决方法使用两个 NSDateFormatter,一个用于日期,一个用于时间。看来,当您使用 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]];
}

【问题讨论】:

    标签: iphone nsdateformatter


    【解决方案1】:

    这种行为的原因是区域设置,设置正确的区域设置,将 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”?

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题。我已将其作为 Apple 的错误提出,并将其发布在 Open Radar 上:

      http://openradar.appspot.com/9774877

      【讨论】:

      【解决方案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
      

      这解决了我的两个问题:

      1. 当用户设置为 12h 显示模式时,使用此格式化程序的输出显示为 24h
      2. 当您手动设置为都柏林时区时,输出不会显示带有 Z 的 dateTime 而是 +0000

      【讨论】:

        【解决方案4】:

        我有一个类似的问题,我最终做的是修改 dateFormat,而不是指定 timeStyle。

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

        对我来说效果很好,希望对你有帮助。

        【讨论】:

          【解决方案5】:

          使用这种格式

          NSDateFormatter* relativeDateFormatter = [[NSDateFormatter alloc] init];
          dateFormatter.dateFormat = @"yyyy-MM-dd hh:mm a";
          
          NSString *stringDate = [dateFormatter stringFromDate:[NSDate date]];
          
          1. HH (一天中的小时 (0-23)) 使用 hh (上午/下午的小时 (1 -12))
          2. 在格式末尾添加 a (Am/pm marker)。

          例如:[NSDate date] 是 2017-05-14 12:40:00

          • 在 24 小时内您将获得 2017-05-14 12:40
          • 在 12 小时后,您将获得 2017-05-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];
          

          【讨论】:

          • 请注意,在现代 iOS 中,Apple 推荐使用日期格式化程序的唯一方法是:stackoverflow.com/a/42370648/294884
          • 我认为如何解释 HH 和 hh 的行为取决于语言环境的默认 12/24 小时切换设置。如果您在一个默认为 12 小时的区域,它会将 hh a 更改为 HH 但不会反过来,如果您在一个默认为 24​​ 小时的区域,它将 HH 更改为 hh a 而不是另一个一路走来……
          猜你喜欢
          • 1970-01-01
          • 2021-12-08
          • 2015-06-02
          • 2012-01-10
          • 1970-01-01
          • 1970-01-01
          • 2015-06-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多