显示24小时制,0

时间:2019-10-13 11:42:28

标签: swift datetime time

我正在尝试修正时间显示。我目前将其显示为24小时,但没有显示code,因此它将显示0而不是1:5

01:05

如何格式化显示前override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) { let dateTime = Date() let cal = Calendar.current let dfm = DateFormatter() dfm.dateFormat = "MMM" let time = String(cal.component(.hour, from: dateTime)) + ":" + String(cal.component(.minute, from: dateTime)) let month = dfm.string(from: dateTime) let date = String(cal.component(.day, from: dateTime)) + " " + month timeNow.text = time dateNow.text = date .uppercased() self.dateTimeView.alpha = 1 DispatchQueue.main.asyncAfter(deadline: .now() + 3) { UIView .transition(with: self.dateTimeView, duration: 0.5, options: UIView.AnimationOptions.curveEaseIn, animations: {self.dateTimeView.alpha = 0}, completion: nil) } 的时间?

1 个答案:

答案 0 :(得分:0)

您似乎了解DateFormatter,但仅将其用于格式化月份。您也可以使用它来格式化其他内容,例如天,小时和分钟。

let dateTime = Date()
let cal = Calendar.current
let dfm = DateFormatter()
dfm.dateFormat = "dd MMM" // this is the format of the date string you need

let date = dfm.string(from: dateTime).uppercased()

dfm.dateFormat = "HH:mm" // this is the format of the time string you need

let time = dfm.string(from: dateTime)

timeNow.text = time
dateNow.text = date