Swift Convert String to Date

63 Views Asked by At

I have a string with time in it "20:52" and I am trying to use DateFormatter to convert it to Date.

My code is:

let dateFormatter = DateFormatter()
dateFormatter.locale = Locale.current
dateFormatter.dateFormat = "HH:mm"
var dateFromStr = dateFormatter.date(from:"20:52")!
print("dateFromStr: \(dateFromStr)")

And the result printed is: dateFromStr: 2000-01-02 02:52:00 +0000 but I would expect it to be dateFromStr: 2000-01-02 20:52:00 +0000

What am I missing?

0

There are 0 best solutions below