gpt4 book ai didi

ios - 将时间转换为 HH :mm:ss formate

转载 作者:行者123 更新时间:2023-11-29 05:58:16 25 4
gpt4 key购买 nike

我正在将时间转换为这种格式(HH:mm:ss),但我得到的响应为零。这里我附上了我的完整代码,请提出一些建议。

// This is my time
let setDate = "2000-01-01 12:04:00 UTC"

// Conversion in seconds
let secondsConvert = manyServiceTime*60*60
print(secondsConvert)

// Time Interval
let datebbb = setDate!.addingTimeInterval(TimeInterval(secondsConvert))
print(datebbb)

//this is I am getting date with am/pm
let formatterEnd = DateFormatter()
formatterEnd.dateFormat = "hh:mm a"
formatterEnd.timeZone = TimeZone(abbreviation: "UTC")
let settime1 = formatterEnd.string(from: datebbb)
print(settime1)

//now here I am converting into hours minutes and seconds.
let formatEnd = DateFormatter()
formatEnd.dateFormat = "HH:mm:ss"
let endTimeSet = formatEnd.string(from: datebbb)
print(endTimeSet)

最佳答案

如果您只想要时间,请使用此代码我创建了一个以字符串形式返回时间的扩展

extension String {

func formatDate() -> String {
let formatter = DateFormatter()
formatter.timeZone = TimeZone(abbreviation: "UTC")
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateFromStr = formatter.date(from: self)

formatter.dateFormat = "HH:mm:ss"
let endTimeSet = formatter.string(from: dateFromStr!)
print(endTimeSet)
return endTimeSet
}
}

使用

let setDate = "2000-01-01 12:04:00"
let timeOnly = setDate.formatDate()

关于ios - 将时间转换为 HH :mm:ss formate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54919880/

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