I am struggling with Date
and I'm assuming is TimeZone
.
Currently I get from my backend a string like this "2020-04-07"
and when I try to convert it to date it turns into 2020-04-06 22:00:00 +0000
. I am in Spain (UTC+2) which I guess this is why it removes 2 hours?
This is my date formatter:
var dateFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.timeZone = TimeZone.current
return dateFormatter
}()
And I call it dateFormatter.date(from: startDateString)
I am setting my current timezone but seems to be ignoring it or am I missing something?
I have followed a lot of answers from here but it's always the same result.
Thank you
The Date
object does not have any inherent locale / time zone. It just represents a moment in time. If you want to see that Date
as a string in a specific locale/time zone you have to use a date formatter. Or there's descriptionWithLocale
. If you use print
it will print a debug description of the Date
instance in UTC.