gpt4 book ai didi

ios - 如果系统是 24 小时格式,Swift 如何获得 12 小时格式

转载 作者:搜寻专家 更新时间:2023-10-31 22:20:03 24 4
gpt4 key购买 nike

<分区>

我的 iPhone 设备系统设置为 24 小时制我想获得 12 小时制,即:23:27 获得 11:27 PM

我试过:

            let today = NSDate()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "hh:mm a"
let string = dateFormatter.string(from: today as Date) // 23:27

得到 23:27 字符串。

我找到了这个:How to get 12 hour format time string when system is set to use 24 hour format

            let today = NSDate()
let dateFormatter = DateFormatter()
let dateFormat = DateFormatter.dateFormat(fromTemplate:"hh:mm a", options: 0, locale: NSLocale.current)
dateFormatter.dateFormat = dateFormat
let string = dateFormatter.string(from: today as Date) //23:27 i want 11:27 pm

但它不起作用我仍然得到 23:27 字符串而不是 11:27pm 字符串。

谢谢!

编辑:根据 shallowThought 的回答不确定 Locale 是否是这种情况?

enter image description here

编辑2添加这个:

dateFormatter1.locale = Locale(identifier: "en_US_POSIX")

有效。我当前的语言环境是 en_CN

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