How can I parse a date that has only date information (without time), like 2018-07-24
Im trying with
let dateFormatter = DateFormatter()
dateFormat.dateFormat = "yyyy-MM-dd"
dateFormatter.dateFormat = dateFormat
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
dateFormatter.date(from: "2018-07-24")
prints day 23
It looks like it uses 00:00:00
as default time and then transform it into UTC results into the day before...
How can I change that?
Seems that you set time zone for formatter to UTC but then try to get the day in your local time zone.
If you use this code - you will see the 24-th day
But if you use "Calendar" to get the day component from the date you will get the day with your timeZone offset.
So for example in my time zone I see the 24th day (GMT+02) using this code
But if I set the time zone for calendar somewhere in USA I see the 23rd day
So the calendar uses your local time zone to get the component from date
So if you need to get the day component from date use the same time zone you used to parse the date from string.