无法使用AM / PM
转换字符串到日期格式我的示例代码:
func encodeWithCoder(aCoder: NSCoder) {
....
aCoder.encodeObject(addresss, forKey: "addresss")
}
输入为:let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
if let todaysDate:Date = dateFormatter.date(from: dateString) {
let newdateFormatter:DateFormatter = DateFormatter()
newdateFormatter.dateFormat = "dd MMMM yyy hh:mm a"
newdateFormatter.amSymbol = "AM"
newdateFormatter.pmSymbol = "PM"
return newdateFormatter.string(from: todaysDate)
}
输出为:"2017-01-01 13:40:15.314"
预期结果是:01 January 2017 19:57
答案 0 :(得分:1)
将日期格式化程序的区域设置设置为en_US_POSIX
,作为副作用,它也将AM / PM符号设置为大写。
let newdateFormatter = DateFormatter() // Do not annotate types the compiler can infer
newdateFormatter.locale = Locale(identifier: "en_US_POSIX")
newdateFormatter.dateFormat = "dd MMMM yyyy h:mm a"
newdateFormatter.string(from: todaysDate)