0

I have following UTC like 2020-07-15T12:32:38+00:00. I actually need to convert this string to local date in phone taking into account a timezone. Here is my try that I see from other stackoverflow answers:

func UTCToLocal(date: String) -> Date? {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
    //I thought than below can help 
    dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
    return dateFormatter.date(from: date)
}

Another approach:

func UTCToLocal2(date: String) -> Date? {
   let dateFormatter = DateFormatter()
   dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
   /// I thought that specifying current time zone would help 
   dateFormatter.timeZone = TimeZone.current
   return dateFormatter.date(from: date)
}

But none of approach works. My 2020-07-15T12:32:38+00:00 is not converted to date with my timezone. Where is my mistake?

neo
  • 1,314
  • 2
  • 14
  • 34
  • 2
    There is no such thing as a “local date”. A `Date` is an absolute point in time, independent of timezones, calendars, etc. If you want a *representation* of the date in your timezone then you need to convert it to a *string* (using a date formatter). – Martin R Jul 15 '20 at 13:04
  • Essentially a duplicate of https://stackoverflow.com/q/40438550/1187415. – Martin R Jul 15 '20 at 13:06

0 Answers0