gpt4 book ai didi

swift - 显示 24 小时时间,带 0

转载 作者:行者123 更新时间:2023-11-30 10:34:31 25 4
gpt4 key购买 nike

我正在尝试修复我的时间显示。我目前将其显示为 24 小时,但不显示 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)

}

如何格式化时间以显示前导 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

关于swift - 显示 24 小时时间,带 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58363224/

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