I have string values include time with formatted "HH:mm" (16:50)
i have to convert this value to "hh:mm" format (04:50) as a string in SWIFT
i have tried NSDateFormatter like
let timeFormat = NSDateFormatter()
timeFormat.dateFormat = "hh:mm"
var dateFromStr = timeFormat.dateFromString("16:50")
var strFromDate = timeFormat.stringFromDate(dateFromStr)
but its not working.. Please tell any solution.
Convert DateTime to String using the ToString() MethodUse the DateTime. ToString() method to convert the date object to string with the local culture format. The value of the DateTime object is formatted using the pattern defined by the DateTimeFormatInfo.
The class we use to convert a string to a date in Swift is DateFormatter (or NSDateFormatter if you are using Objective-C).
I solved my problem to the format yyyy-MM-dd'T'HH:mm:ss. SSS'Z' (e.g 2018-06-15T00:00:00.000Z) with this: func formatDate(date: String) -> String { let dateFormatterGet = DateFormatter() dateFormatterGet. dateFormat = "yyyy-MM-dd'T'HH:mm:ss.
"en_US_POSIX" is also invariant in time (if the US, at some point in the future, changes the way it formats dates, "en_US" will change to reflect the new behaviour, but "en_US_POSIX" will not), and between machines ("en_US_POSIX" works the same on iOS as it does on OS X, and as it it does on other platforms).
You need 2 NSDateFormatter
s, one for dateFromString
, one for 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 | SWIFT 5.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)
}
usage:
let outStr = dateTimeChangeFormat(str: "16:50",
inDateFormat: "HH:mm",
outDateFormat: "hh:mm")
println(outStr) // -> outputs 04:50
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With