Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 986
isyap Avatar asked Feb 20 '15 08:02

isyap


People also ask

How do you convert time to string?

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.

How do I convert a string to a date in Swift?

The class we use to convert a string to a date in Swift is DateFormatter (or NSDateFormatter if you are using Objective-C).

How do I change the date format in swift5?

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.

What is en_US_POSIX?

"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).


2 Answers

You need 2 NSDateFormatters, 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)
like image 52
rintaro Avatar answered Oct 05 '22 17:10

rintaro


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
like image 34
Andrew Avatar answered Oct 05 '22 16:10

Andrew