我正在将时间转换为这种形式(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)
答案 0 :(得分:0)
如果只想使用此代码 我创建了一个扩展,该扩展以字符串形式返回时间
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()