且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在日期和时间中正确支持iOS 12小时/24小时时间覆盖DateFormatters的时间设置?

更新时间:2023-02-26 16:25:12

来自 DateFormatter dateFormat的文档:

仅应在使用固定格式时设置此属性 表示形式,如使用固定格式日期中所述 制图表达.对于用户可见的表示形式,您应该使用 dateStyle和timeStyle属性,或者 setLocalizedDateFormatFromTemplate(_ :)方法(如果需要的格式) 使用预定义的样式无法实现;这两个 属性和此方法提供了本地化的日期表示形式 适合向用户显示.

You should only set this property when working with fixed format representations, as discussed in Working With Fixed Format Date Representations. For user-visible representations, you should use the dateStyle and timeStyle properties, or the setLocalizedDateFormatFromTemplate(_:) method if your desired format cannot be achieved using the predefined styles; both of these properties and this method provide a localized date representation appropriate for display to the user.

我仍然不太明白为什么如果您不在日期格式化程序上使用固定的语言环境,基金会为什么会麻烦修改日期格式,但是似乎文档不建议您将自定义日期格式用于非固定格式表示形式.

I still don't really understand why Foundation bothers to modify the date format if you're not using a fixed locale on your date formatter, but it seems the documentation does not recommend using the custom date format for non-fixed format representations.

setLocalizedDateFormatFromTemplate,这似乎是您所需要的,尽管它似乎不会随着用户的24小时制自动改变,因此可以检查在指定模板之前先查看是否有24小时开放时间……

setLocalizedDateFormatFromTemplate appears to be what you need if you can't just rely on dateStyle and timeStyle, although it doesn't appear to automatically change with a user's 24 hour time setting, so one could check to see if 24 hour time is on before specifying a template…

该方法应该允许您指定一种格式,而不会丢失每种语言环境特定的格式.

This method is supposed to allow you to specify a format without losing out on the formatting that is specific to each locale.

Apple Developer Relations的EDIT响应(相同的想法,但是更多的细节):

EDIT response from Apple Developer Relations (same idea, but more detail):

处理此问题的正确方法是避免指定"hh"或"HH" 基于语言环境,而是使用"jj"框架符号 与-[NSDateFormatter setLocalizedDateFormatFromTemplate:]一起使用.

The correct way to handle this is to avoid specifying "hh" or "HH" based on locale and instead using the "jj" skeleton symbol in conjunction with -[NSDateFormatter setLocalizedDateFormatFromTemplate:].

从日期"字段符号表的小时"部分开始 ( https://www.unicode.org/reports/tr35/tr35-dates.html#dfst-hour ):

From the "hour" section of the Date Field Symbol Table (https://www.unicode.org/reports/tr35/tr35-dates.html#dfst-hour):

输入骨架符号在这种情况下,它要求 语言环境的首选小时格式(h,H,K或K),由 补充数据中小时"元素的首选属性.

Input skeleton symbol In such a context, it requests the preferred hour format for the locale (h, H, K, or K), as determined by the preferred attribute of the hours element in the supplemental data.

通过使用"jj",您可以获得与用户的格式最匹配的格式 当前设置:

By using "jj" you can get a format which best matches the user's current settings:

NSDateFormatter *formatter = [NSDateFormatter new];
[formatter setLocalizedDateFormatFromTemplate:@"jj:mm"];
NSLog(@"%@: 'jj:mm' => '%@' ('%@')", formatter.locale.localeIdentifier, formatter.dateFormat, [formatter stringFromDate:[NSDate date]]);

产生

  • en_US(12小时):"en_US:'jj:mm'=>'h:mm a'('1:44 PM')
  • en_US(24小时):"en_US:'jj:mm'=>'HH:mm'('13:44')
  • en_GB(12小时):"en_GB:'jj:mm'=>'h:mm a'('1:44 pm')
  • en_GB(24小时):"en_GB:'jj:mm'=>'HH:mm'('13:44')
  • en_US (12-hour): "en_US: 'jj:mm' => 'h:mm a' ('1:44 PM')
  • en_US (24-hour): "en_US: 'jj:mm' => 'HH:mm' ('13:44')
  • en_GB (12-hour): "en_GB: 'jj:mm' => 'h:mm a' ('1:44 pm')
  • en_GB (24-hour): "en_GB: 'jj:mm' => 'HH:mm' ('13:44')

这可以让您匹配首选格式,同时保持 用户的首选时间设置.

This allows you to match your preferred format while keeping with the user's preferred time settings.