gpt4 book ai didi

swift - 日期格式模板中区域设置的首选小时格式

转载 作者:行者123 更新时间:2023-11-28 05:49:39 26 4
gpt4 key购买 nike

当使用 Foundation 的 DateFormatter 为区域设置“de”从模板生成日期格式时,如下所示:

let locale: Locale = Locale(identifier: "de")
let format = DateFormatter.dateFormat(fromTemplate: "dMMMyGhhmmss",
options: 0,
locale: locale)

我总是得到这种格式:

d. MMM y G, h:mm:ss a

渲染

17. Nov. 2018 n. Chr., 8:30:20 PM

“PM”部分对于语言环境“de”来说并不常见,但据我了解,此方法还考虑了用户偏好。对吗?

我尝试了系统偏好设置中的设置,例如切换到 24 小时制,但我所做的任何更改都没有任何效果。是否必须重新启动才能使更改生效?

谁能给我解释一下这是怎么回事?

最佳答案

如果您在模板中使用 hH,格式将包含指定的小时格式(12 小时或 24 小时),而不管区域设置。请改用 j。此符号请求区域设置的首选小时格式。

例如,给定模板 dMMMyGjjmmss,我得到:

  • MMM d, y G, h:mm:ss a(12 小时格式)用于语言环境“en”
  • d. MMM y G,HH:mm:ss(24 小时格式)用于语言环境“de”

Apple's documentation状态:

The format string uses the format patterns from the Unicode Technical Standard #35.

Unicode Technical Standard #35关于符号 j 有这样的说法:

This is a special-purpose symbol. It must not occur in pattern or skeleton data. Instead, it is reserved for use in skeletons passed to APIs doing flexible date pattern generation. In such a context, it requests the preferred hour format for the locale (h, H, K, or k), as determined by whether h, H, K, or k is used in the standard short time format for the locale. In the implementation of such an API, 'j' must be replaced by h, H, K, or k before beginning a match against availableFormats data. Note that use of 'j' in a skeleton passed to an API is the only way to have a skeleton request a locale's preferred time cycle type (12-hour or 24-hour).

关于swift - 日期格式模板中区域设置的首选小时格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53354946/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com