Convert UTC Timezone To Local/Device Current Timezone and Vice-Versa Swift 4 - iOS - Swift 4 Tutorials W3Schools

Hot

Post Top Ad

10 Oct 2017

Convert UTC Timezone To Local/Device Current Timezone and Vice-Versa Swift 4 - iOS

In this article we are going to convert UTC Date format to Current device date format.

UTC is the time standard commonly used across the world. The world's timing centers have agreed to keep their time scales closely synchronized - or coordinated - therefore the name Coordinated Universal Time.

Convert UTC Timezone To Local/Device Current Timezone and Vice-Versa Swift 4 - iOS

I know that you guys are totally confused of this date Timezone conversions.

Here we simply use following method to convert UTC to local.

UTC to Local:

Use the following method for converting:
func UTCToLocal(UTCDateString: String) -> String {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss" //Input Format
    dateFormatter.timeZone = NSTimeZone(name: "UTC") as TimeZone!
    let UTCDate = dateFormatter.date(from: UTCDateString)
    dateFormatter.dateFormat = "yyyy-MMM-dd hh:mm:ss" // Output Format
    dateFormatter.timeZone = TimeZone.current
    let UTCToCurrentFormat = dateFormatter.string(from: UTCDate!)
    return UTCToCurrentFormat
}

Usage:

Call above method as follow:
let dateString = "2017-10-10 9:56:25"

let date = self.UTCToLocal(UTCDateString: dateString)

//output date should be '2017-10-10 15:26:25'

In above method we can customize date formats as per our usage. But the input date format must be same format as input date string.

Local to UTC:

Use the following method for converting:
func localToUTC(date:String) -> String {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
    dateFormatter.calendar = NSCalendar.current
    dateFormatter.timeZone = TimeZone.current
    
    let dt = dateFormatter.date(from: date)
    dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
    dateFormatter.dateFormat = "yyyy-MMM-dd hh:mm:ss"
    
    return dateFormatter.string(from: dt!)
}

Usage:

Call above method as follow:
let dateString = "2017-10-10 15:56:25"

let date = self.localToUTC(date: dateString)

//output date should be '2017-10-10 09:26:25'

No comments:

Post a Comment

Post Top Ad