Maitery Trivedi
Maitery Trivedi

Reputation: 21

UTC Date To Current TimeZone String and Result StringDate to Current TimeZone Date

I get the current date in UTC and I converted it into Local TimeZone that result comes in string.

Now, I converted that Result String Date into a Date with the same Local TimeZone but it gives the date in UTC.

Below is the Code I am using:

    print("current date :\( Date())") //current date : 2017-03-08 11:11:12 +0000

    let dateFormatter = DateFormatter()
    dateFormatter.timeZone = TimeZone.current
    dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss Z"
    let localDate : String = dateFormatter.string(from: Date())

    print("localDate date :\(localDate)") // localDate date: 08-03-2017 16:41:12 +0530

    let dateFormatter1 = DateFormatter()
    dateFormatter1.timeZone = TimeZone.current
    dateFormatter1.dateFormat = "dd-MM-yyyy HH:mm:ss Z"
    let dateCurrent = dateFormatter1.date(from: localDate)!

    print("dateCurrent date :\(dateCurrent)") // dateCurrent date: 2017-03-08 11:11:12 +0000

dateCurrent

I want in

08-03-2017 16:41:12 +0530

only of type Date not of type String.

Where have I gone wrong?

Upvotes: 1

Views: 115

Answers (4)

Maitery Trivedi
Maitery Trivedi

Reputation: 21

Thanks everyone for your Answers,

I dug out and I found the solution. Here I am sharing the code.

    let currentDate = Date()

    let CurrentTimeZone: TimeZone = TimeZone(abbreviation: "UTC")!
    let SystemTimeZone: TimeZone = TimeZone.current

    let currentGMTOffset: Int = CurrentTimeZone.secondsFromGMT(for: currentDate)
    let SystemGMTOffset: Int = SystemTimeZone.secondsFromGMT(for: currentDate)

    let interval: TimeInterval = Double(SystemGMTOffset) - Double(currentGMTOffset)

    let todayDate: Date = Date(timeInterval: interval, since: currentDate)

    print("  Today Date : \(todayDate)")

Upvotes: 1

Nirmalsinh Rathod
Nirmalsinh Rathod

Reputation: 5186

Issue with your dateFormatter. Here is my code to conver UTC to Local and Local to UTC:

    convertUTCtoLocal(strUTCDate: "09-03-2017 03:54:00")
    convertLocalToUTC(strUTCDate: "09-03-2017 09:24:00")

Convert UTC to Local

    func  convertUTCtoLocal(strUTCDate: String){
    let dateFormater : DateFormatter = DateFormatter();
    dateFormater.timeZone = TimeZone(abbreviation: "UTC")
    dateFormater.dateFormat = "dd-MM-yyyy HH:mm:ss"

    let dateFromString = dateFormater.date(from: strUTCDate)

    dateFormater.timeZone = NSTimeZone.local as TimeZone!
    dateFormater.dateFormat = "dd-MM-yyyy HH:mm:ss"

    let localData = dateFormater.string(from: dateFromString!)
    print("Your local date is \(localData)")        
}

Convert Local to UTC

   func  convertLocalToUTC(strUTCDate: String){
    let dateFormater : DateFormatter = DateFormatter();
    dateFormater.dateFormat = "dd-MM-yyyy HH:mm:ss"
    dateFormater.timeZone = NSTimeZone.local as TimeZone!

    let dateFromString = dateFormater.date(from: strUTCDate)

    dateFormater.timeZone = NSTimeZone(abbreviation: "UTC") as TimeZone!;
    dateFormater.dateFormat = "dd-MM-yyyy HH:mm:ss"

    let localData = dateFormater.string(from: dateFromString!)
    print("Your UTC date is \(localData)")        
}

Upvotes: 0

Rahuld
Rahuld

Reputation: 357

try this, Hope it helps

let date: String =  "dd-MM-yyyy HH:mm:ss Z"
    var dateArr = date.components(separatedBy: " ")
    let dateCurrent  = (dateArr [0] )
    print(dateCurrent)

Upvotes: 1

George Green
George Green

Reputation: 4905

This is how Date objects work. You simply keep the date object until you need to output it to the user or an API, at which point you can convert it to a string in the appropriate time zone.

A Date object essentially just wraps a unix timestamp with some helper methods for dealing with it.

Upvotes: 0

Related Questions