我正在将时间转换为 (hh:mm a) 格式。它在所有其他版本上工作正常,但在 11.1.1 上出现错误
当我将当前的 DateFormatter 更改为时间时,它仅给出 hh:mm
let dateFormatter: DateFormatter = DateFormatter()
dateFormatter.dateFormat = "hh:mm a"
// Set date format
// dateFormatter.timeStyle = .short
// Apply date format
let selectedtime: String = dateFormatter.string(from: sender.date)
self.timeTextField.text = selectedtime
//// It gives 02:57 only What is missing although it is working for all other version.
提前致谢!
最佳答案
我在 iOS 11.0.3 上遇到了同样的问题。 我的时间格式“h:mm a”或“hh:mm a”的工作方式绝对类似于“hh:mm”,即显示“21:00”而不是“9:00 PM”或“09:00 PM”。
如何解决这个问题:
如果允许“21:00 PM”格式,您可以在日期格式字符串中将小时标识符“h”大写,并且“H:mm a”或“HH:mm a”格式都可以!
但如果没有,请设置 dateFormatter 的“locale”属性。就我而言,它是
dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_GB"];
Swift 解决方案: dateFormatter.locale = Locale(identifier: "en_GB")
之后我得到了“9:00 pm”字符串,而不是错误的“21:00”。
或者
dateFormatter.locale = NSLocale.system
关于ios - 时间格式(swift 4 中的 hh :mm a) not working for iOS 11. 1.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48180413/