matarali
matarali

Reputation: 127

Convert an array of times from 24hr to 12 hr in Swift

I have an array like this:

arrayTimes = ["16:00", "16:30", "17:00", "17:30", "18:00", "18:30"]

and I want to convert the array from 24 hr to 12 hr.. This is what I have so far which converts the first number in the array.. I'm missing something but I can't work it out.

func convertTimes(){

                for twelve in arrayTimes{

                    var two = arrayTimes[0]

                    let dateFormatter = NSDateFormatter()
                    dateFormatter.dateFormat = "H:mm"
                    let date12 = dateFormatter.dateFromString(two)!

                    dateFormatter.dateFormat = "h:mm a"
                    let date22 = dateFormatter.stringFromDate(date12)

                    print(date22)

                    print("output \(twelve)")
                }
                }

            convertTimes()

Upvotes: 3

Views: 7201

Answers (2)

Leo Dabus
Leo Dabus

Reputation: 236370

If you would like to change your array you can use enumerate and use that index to change the array element:

Swift 3 or later

var arrayTimes = ["16:00", "16:30", "17:00", "17:30", "18:00", "18:30"]

let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
for (index,time) in arrayTimes.enumerated() {
    dateFormatter.dateFormat = "H:mm"
    if let inDate = dateFormatter.date(from: time) {
        dateFormatter.dateFormat = "h:mm a"
        let outTime = dateFormatter.string(from:inDate)
        print("in \(time)")
        print("out \(outTime)")
        arrayTimes[index] = outTime
    }

}

print(arrayTimes)  // "4:00 PM", "4:30 PM", "5:00 PM", "5:30 PM", "6:00 PM", "6:30 PM"]

Swift 2

var arrayTimes = ["16:00", "16:30", "17:00", "17:30", "18:00", "18:30"]

let dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
for (index,time) in arrayTimes.enumerate() {
    dateFormatter.dateFormat = "H:mm"
    if let inDate = dateFormatter.dateFromString(time) {
        dateFormatter.dateFormat = "h:mm a"
        let outTime = dateFormatter.stringFromDate(inDate)
        print("in \(time)")
        print("out \(outTime)")
        arrayTimes[index] = outTime
    }

}


print(arrayTimes)  // "4:00 PM", "4:30 PM", "5:00 PM", "5:30 PM", "6:00 PM", "6:30 PM"]

Upvotes: 5

Eric Aya
Eric Aya

Reputation: 70098

You're not using the right value for the conversion: your two variable is always the first item of the array.

Just use twelve instead, which represents each item in the array while looping:

for twelve in arrayTimes {

    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "H:mm"
    let date12 = dateFormatter.dateFromString(twelve)!

    dateFormatter.dateFormat = "h:mm a"
    let date22 = dateFormatter.stringFromDate(date12)

    print(date22)

    print("output \(twelve)")
}

Also, just a tip: it's not needed to create a new formatter each time the loops iterates, you can declare the formatter only once, outside the loop. And be careful with force-unwrapped optionals, I prefer to use if let or any other known method like guard.

Example with if let:

let dateFormatter = NSDateFormatter()

for twelve in arrayTimes {

    dateFormatter.dateFormat = "H:mm"
    if let date12 = dateFormatter.dateFromString(twelve) {
        dateFormatter.dateFormat = "h:mm a"
        let date22 = dateFormatter.stringFromDate(date12)
        print(date22)
        print("output \(twelve)")
    } else {
        // oops, error while converting the string
    }

}

Upvotes: 8

Related Questions