8

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.

3
  • Just a wild guess.... shouldn't it be var date_from_str = time_format.dateFromString("16:50") ? Commented Feb 20, 2015 at 8:55
  • why my question get downvote? and first answer erased from page while thats true.. what is going on here Commented Feb 20, 2015 at 12:32
  • I don't mean to sound rude, but did you look at the stackoverflow.com/tour ? Commented Feb 20, 2015 at 12:53

6 Answers 6

15

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)
Sign up to request clarification or add additional context in comments.

Comments

7

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

Comments

2

Can be achieved by using one DateFormatter.

Plus added some extra format in one place. Hope someone get help.

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

Comments

1

I believe you should write the time inside the string.

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

Comments

0

This is an old post, but I think it's worth noting that isLenient allows you to use 1 formatter instead of 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"

Comments

-1
 func hrs12FormateString(date:String)-> String {
        let inFormatter = DateFormatter()
        inFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX") as Locale
        inFormatter.dateFormat = "HH:mm"

        let outFormatter = DateFormatter()
        outFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX") as Locale
        outFormatter.dateFormat = "hh:mm a"  
        let inStr = date 
        let date = inFormatter.date(from: inStr)!
        let outStr = outFormatter.string(from: date)
        return outStr
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.