Search code examples
iosswift4

Convert UTC date to local date swift4


All,

I am trying to convert UTC date to local date . Below is my code. But, even after converting I get both dates in UTC only.

 static func getTodayDateInLocalTimeZone() -> Date{
    
    let todaydateInUTC = Date()
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
    dateFormatter.timeZone = TimeZone(identifier: "UTC")
    let utcDateString = dateFormatter.string(from: todaydateInUTC)
    print("Date::: utcDateString: \(utcDateString)")
    
    
    // Changing to Current timezone
    let timzoneIdentiier = TimeZone.current.identifier
    let timezone = TimeZone(identifier: timzoneIdentiier)
    let abbrv = timezone?.abbreviation()
    
    let dateFormatter1 = DateFormatter()
    dateFormatter1.timeZone =  TimeZone(identifier: abbrv!)
    dateFormatter1.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
   
    print("Date:::timeZone: \(timzoneIdentiier) == \(String(describing: abbrv))")
    let formattedDate1  = dateFormatter1.date(from: utcDateString)
    print("Date:::: \(formattedDate1)") 
    return formattedDate1!
}

Here is what I get when I print

Date::: utcDateString: 2021-01-20T17:39:15+0000
Date:::timeZone: America/New_York == Optional("EST")
Date:::: Optional(2021-01-20 17:39:15 +0000)

Please let me know why is it now changing to the local timezone. Thanks


Solution

  • First of all, a date does not have a time zone.

    So let todaydateInUTC = Date() actually means let todaydate = Date().

    The time zone becomes relevant, when you want to present a date to the user. So instead of creating a new date from a the utcDateString, you just need to create another date string from the same date variable. let formattedDate1 = dateFormatter1.date(from: utcDateString) becomes let tzDateString = dateFormatter1.string(from: todaydate).

    This also means your function should return a string instead of a date.

    For example:

    func getTodayDateInLocalTimeZone() -> String
    {
        let now = Date()
        
        // Just for debugging. Not for the result.
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
        dateFormatter.timeZone = TimeZone(identifier: "UTC")
        let utcDateString = dateFormatter.string(from: now)
        print("Date::: utcDateString: \(utcDateString)")
        
        let tz = TimeZone.current
        let dateFormatter1 = DateFormatter()
        dateFormatter1.timeZone =  tz
        dateFormatter1.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
        print("Date:::timeZone: \(tz) == \(String(describing: tz.abbreviation()))")
        
        let tzDateString  = dateFormatter1.string(from: now)
        print("Date:::: \(tzDateString)")
        return tzDateString
    }
    

    For me it results in:

    Date::: utcDateString: 2021-01-20T18:19:44+0000
    Date:::timeZone: Europe/Berlin (current) == Optional("CET")
    Date:::: 2021-01-20T19:19:44+0100