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 seem to work if I put the time alone
let dateAsString = "02/12/15, 6:35 PM"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "HH"
let date = dateFormatter.dateFromString(dateAsString) //returns nil
Does anyone know how to accomplish this?
Thank you.
Here is code for other way around
For Swift 3
Below is the swift 3 version of the solution -
Swift version 3.0.2 , Xcode Version 8.2.1 (8C1002) (12 hr format ):
OUTPUT : 12:41:42 AM
Feel free to comment. Thanks
Just convert it to a date using NSDateFormatter and the "h:mm a" format and convert it back to a string using the "HH:mm" format. Check out this date formatting guide to familiarize yourself with this material.
Swift 3
Time format 24 hours to 12 hours
output will be 12 hour formatted Date: 1:15 PM
Time format 12 hours to 24 hours
output will be 24 hour formatted Date: 13:15
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...
After writing this function you can call this where you are choosing the time from date/time picker.
Thanks, Hope this helped.