ios - 时间格式(swift 4 中的 hh :mm a) not working for iOS 11. 1.1

标签 ios datepicker swift4 dateformatter

我正在将时间转换为 (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”。

如何解决这个问题:

  1. 如果允许“21:00 PM”格式,您可以在日期格式字符串中将小时标识符“h”大写,并且“H:mm a”或“HH:mm a”格式都可以!

  2. 但如果没有,请设置 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/

相关文章:

iphone - iOS - UIScrollView 不更新其内容,一些 UIViews 在重新加载后消失

ios - 将十六进制颜色字符串转换为 Unsigned Long

ios - 如何在 Swift 中创建动态大小的 UITableViewCell?

ios - 将 Xib 添加为 subview 时,Xib 框架大小不正确

从 Swift 字符串转换为 const char*

iphone - 自定义 UITableViewCell 中的自定义 UIView

javascript - jQuery 日期选择器函数 : datepicker not a function

javascript - Microsoft JScript 运行时错误 : Out of stack space in date. js

javascript - 谷歌脚本: Create Datepicker for Date Input

swift - 将 ARKit 场景保存到后端并稍后加载?