Swift : 如何把String转成时间格式的字符串?

Swift : How to convert String to string with time format?

我的字符串值包括时间,格式为 "HH:mm" (16:50)

我必须将此值转换为 "hh:mm" 格式 (04:50) 作为 SWIFT

中的字符串

我试过像

这样的 NSDateFormatter
let timeFormat = NSDateFormatter()
timeFormat.dateFormat = "hh:mm"
var dateFromStr = timeFormat.dateFromString("16:50")
var strFromDate = timeFormat.stringFromDate(dateFromStr)

但它不起作用.. 有什么解决办法请说下。

我相信你应该把时间写在字符串里面。

    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "hh:mm"
    var date = dateFormatter.dateFromString("00:00")
    var str_from_date = dateFormatter.stringFromDate (date)

您需要 2 个 NSDateFormatter,一个用于 dateFromString,一个用于 stringFromDate

let inFormatter = NSDateFormatter()
inFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
inFormatter.dateFormat = "HH:mm"

let outFormatter = NSDateFormatter()
outFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
outFormatter.dateFormat = "hh:mm"

let inStr = "16:50"
let date = inFormatter.dateFromString(inStr)!
let outStr = outFormatter.stringFromDate(date)
println(outStr) // -> outputs 04:50

Swift 5

let inFormatter = DateFormatter()
inFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX") as Locale
inFormatter.dateFormat = "hh:mm:ssa"

let outFormatter = DateFormatter()
outFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX") as Locale
outFormatter.dateFormat = "hh:mm a"
 
let inStr = "16:50"       
let datess = inFormatter.date(from: inStr)!
let outStr = outFormatter.string(from: datess)
print(outstr)

2020 | SWIFT5.1:

func dateTimeChangeFormat(str stringWithDate: String, inDateFormat: String, outDateFormat: String) -> String {
    let inFormatter = DateFormatter()
    inFormatter.locale = Locale(identifier: "en_US_POSIX")
    inFormatter.dateFormat = inDateFormat

    let outFormatter = DateFormatter()
    outFormatter.locale = Locale(identifier: "en_US_POSIX")
    outFormatter.dateFormat = outDateFormat

    let inStr = stringWithDate
    let date = inFormatter.date(from: inStr)!
    return outFormatter.string(from: date)
}

用法:

let outStr = dateTimeChangeFormat(str: "16:50", 
                                  inDateFormat:  "HH:mm", 
                                  outDateFormat: "hh:mm")

println(outStr) // -> outputs 04:50

这是一个旧的 post,但我认为值得注意的是 isLenient 允许您使用 1 个格式化程序而不是 2 个。

func normalizeTimeString(_ original: String) -> String? {
    let formatter = DateFormatter()
    formatter.isLenient = true
    formatter.dateFormat = "hh:mm"

    guard let date = formatter.date(from: original) else { return nil }
    let corrected = formatter.string(from: date)
    return corrected
}

normalizeTimeString("16:50") // Returns "04:50"

可以用一个DateFormatter

Plus 在一个地方添加了一些额外的格式。希望有人得到帮助。

Xcode 12 - Swift 5.3

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
var dateFromStr = dateFormatter.date(from: "16:50")!

// Time with date in long format
dateFormatter.dateFormat = "hh:mm:ss a 'on' MMMM dd, yyyy"
var timeInLongFromDate = dateFormatter.string(from: dateFromStr)
print(timeInLongFromDate)
// 04:50:00 PM on January 01, 2000

// Time with customisable format
dateFormatter.dateFormat = "hh:mm:ss a"
var timeFromDate = dateFormatter.string(from: dateFromStr)
print(timeFromDate)
// 04:50:00 PM

// Customisable AP/PM symbols
dateFormatter.amSymbol = "am"
dateFormatter.pmSymbol = "Pm"
dateFormatter.dateFormat = "a"
var amOrPm = dateFormatter.string(from: dateFromStr)
print(amOrPm)
// Pm