Swift 3.0: converting UTC server time to local time and vice versa

I want to convert the UTC server time to local time and vice versa. Here is my code ..

var isTimeFromServer = true
var time:String!
var period:String!
let timeString = "6:59 AM" //Current UTC time

if isTimeFromServer {

    let index = timeString.index(timeString.startIndex, offsetBy: 5)
    let twelve = timeString.substring(to: index)

    var dateString:String!

    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "H:mm"
    let date12 = dateFormatter.date(from: twelve)!

    dateFormatter.dateFormat = "h:mm a"
    let date22 = dateFormatter.string(from: date12)

    //print(date22)
    dateString = date22
    //print("dateString=\(dateString)")

    time = dateString.components(separatedBy: " ")[0]
    period = dateString.components(separatedBy: " ")[1]

}
else {
    time = timeString.components(separatedBy: " ")[0]
    period = timeString.components(separatedBy: " ")[1]
}

var hour = Int(time.components(separatedBy: ":")[0])

hour = period == "AM" ? hour : hour! + 12
let minute = Int(time.components(separatedBy: ":")[1])
let calender = NSCalendar.current
var datecomponent = DateComponents()
datecomponent.calendar = calender
datecomponent.hour = hour
datecomponent.minute = minute

if !isTimeFromServer {
    // local to UTC
    datecomponent.timeZone = TimeZone.current
}
else {
    datecomponent.timeZone = TimeZone(abbreviation: "UTC")
}

let date = datecomponent.date
let dateFormatter = DateFormatter()

if !isTimeFromServer {
    dateFormatter.dateFormat = "H:mm"
    dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
    dateFormatter.string(from: date!)
}
else {
    //UTC to local
    dateFormatter.dateFormat = "h:mm a"
    dateFormatter.timeZone = TimeZone.current
    dateFormatter.string(from: date!)
}

I get local time

o / n: "12:52 pm"

But the actual local time and the output time difference is 23 minutes.

+23
source share
5 answers

, .
, , . UTCToLocal localToUTC.
. , . , .

func localToUTC(date:String) -> String {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "h:mm a"
    dateFormatter.calendar = NSCalendar.current
    dateFormatter.timeZone = TimeZone.current

    let dt = dateFormatter.date(from: date)
    dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
    dateFormatter.dateFormat = "H:mm:ss"

    return dateFormatter.string(from: dt!)
}

func UTCToLocal(date:String) -> String {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "H:mm:ss"
    dateFormatter.timeZone = TimeZone(abbreviation: "UTC")

    let dt = dateFormatter.date(from: date)
    dateFormatter.timeZone = TimeZone.current
    dateFormatter.dateFormat = "h:mm a"

    return dateFormatter.string(from: dt!)
}

, .

print(UTCToLocal(date: "13:07:00"))
print(localToUTC(date: "06:40 PM"))

, .
!

+62

Mrugesh , - - , , .

func localToUTC(date:String, fromFormat: String, toFormat: String) -> String {

    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = fromFormat
    dateFormatter.calendar = NSCalendar.current
    dateFormatter.timeZone = TimeZone.current
    dateFormatter.date
    let dt = dateFormatter.date(from: date)
    dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
    dateFormatter.dateFormat = toFormat

    return dateFormatter.string(from: dt!)
}

func UTCToLocal(date:String, fromFormat: String, toFormat: String) -> String {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = fromFormat
    dateFormatter.timeZone = TimeZone(abbreviation: "UTC")

    let dt = dateFormatter.date(from: date)
    dateFormatter.timeZone = TimeZone.current
    dateFormatter.dateFormat = toFormat

return dateFormatter.string(from: dt!)
}

let localDateAsString = UTCToLocal(date: dateAsString!, fromFormat: "hh:mm a, dd MMM yyyy", toFormat: "hh:mm a, dd MMM yyyy")

, . , .

+16

Mrugesh Tank Answer,

. ViewController, .

extension String {

  //MARK:- Convert UTC To Local Date by passing date formats value
  func UTCToLocal(incomingFormat: String, outGoingFormat: String) -> String {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = incomingFormat
    dateFormatter.timeZone = TimeZone(abbreviation: "UTC")

    let dt = dateFormatter.date(from: self)
    dateFormatter.timeZone = TimeZone.current
    dateFormatter.dateFormat = outGoingFormat

    return dateFormatter.string(from: dt ?? Date())
  }

  //MARK:- Convert Local To UTC Date by passing date formats value
  func localToUTC(incomingFormat: String, outGoingFormat: String) -> String {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = incomingFormat
    dateFormatter.calendar = NSCalendar.current
    dateFormatter.timeZone = TimeZone.current

    let dt = dateFormatter.date(from: self)
    dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
    dateFormatter.dateFormat = outGoingFormat

    return dateFormatter.string(from: dt ?? Date())
  }
}

: -

Note:- eventStartDate is the string which you have to converted in your format like this:- "2018-07-11T16:22:00.000Z" 

let finalDate = eventStartDate.UTCToLocal(incomingFormat: "yyyy-MM-dd'T'HH:mm:ss.SSSZ", outGoingFormat: "MMM d, yyyy h:mm a")
+7

:

func convertUTCToLocal(timeString: String) -> String? {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "h:mm a"

    dateFormatter.timeZone = TimeZone.init(abbreviation: "UTC")
    let timeUTC = dateFormatter.date(from: timeString)

    if timeUTC != nil {
        dateFormatter.timeZone = NSTimeZone.local

        let localTime = dateFormatter.string(from: timeUTC!)
        return localTime
    }

    return nil
}



func convertLocalToUTC(localTime: String) -> String? {

    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "h:mm a"
    dateFormatter.timeZone = NSTimeZone.local
    let timeLocal = dateFormatter.date(from: localTime)

    if timeLocal != nil {
        dateFormatter.timeZone = TimeZone.init(abbreviation: "UTC")

        let timeUTC = dateFormatter.string(from: timeLocal!)
        return timeUTC
    }
    return nil
}


    var isTimeFromServer = true
    var time:String!
    var period:String!
    let timeString = "6:59 AM" //Current UTC time


    if isTimeFromServer {
        print(convertUTCToLocal(timeString: timeString))
    } else {
        print(convertLocalToUTC(localTime: timeString))
    }
+1

, TimeZone. , , .

, .

, . , , let timeZone = TimeZone(identifier: "Europe/Paris") let timeZone = TimeZone(abbreviation: "CEST") "UTC +2: 00"

CEST, . CET, , "UTC +1: 00".

Date.isDaylightSavingsTime, , , , . ", "

, TimeZone (:...)

0
source

Source: https://habr.com/ru/post/1015749/


All Articles