i have a string like "22:02:20" and need to format it to "10:02:00" i have try this:
let time = "22:02:00"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm:ss"
let calendar = Calendar.current
let unitFlags = Set<Calendar.Component>([.hour, .minute])
staticHoraInicio = calendar.dateComponents(unitFlags, from: dateFormatter.date(from: time))
but i get 22 and 2, how can i get 10 and 02. Also i try:
let horaF = dateFormatter.date(from: time!)
but i get something like "2000-01-02 03:00:00 +0000", ¿there is a way to format just a time string without date?
I need this to store a start time and end time coming from a DB, compare it to the current time to get if it's between, some example about how to do this???
let time = "22:02:00"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm:ss"
var fullDate = dateFormatter.date(from: time)
dateFormatter.dateFormat = "hh:mm:ss a"
var time2 = dateFormatter.string(from: fullDate!)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With