Xcode swift am/pm time to 24 hour format

后端 未结 11 1124
甜味超标
甜味超标 2020-11-28 04:40

I am trying to convert a am/pm format time to a 24 hour format time

6:35 PM to 18:35

I tried this piece of code on playground but it doesnt

11条回答
  •  野性不改
    2020-11-28 05:24

    I am using a function here in my case by which I am updating a label with the normal time format and after that I am storing the selected time's 24hr format to do some another tasks..

    Here is my code...

    func timeUpdate(sender: NSDate)
    {
        let timeSave = NSDateFormatter() //Creating first object to update time label as 12hr format with AM/PM
        timeSave.timeStyle = NSDateFormatterStyle.ShortStyle //Setting the style for the time selection.
        self.TimeShowOutlet.text = timeSave.stringFromDate(sender) // Getting the string from the selected time and updating the label as 1:40 PM
        let timeCheck = NSDateFormatter() //Creating another object to store time in 24hr format.
        timeCheck.dateFormat = "HH:mm:ss" //Setting the format for the time save.
        let time = timeCheck.stringFromDate(sender) //Getting the time string as 13:40:00
        self.timeSelectedForCheckAvailability = time //At last saving the 24hr format time for further task.
    }
    

    After writing this function you can call this where you are choosing the time from date/time picker.

    Thanks, Hope this helped.

提交回复
热议问题