How to Convert Including Timezone Date in Swift

Converting date between timezones swift

Couldn't you just use your data formatter again with a different time zone and convert it? Such as

dateFormatter.timeZone = NSTimeZone(abbreviation: "GMT")
let gmtDate = dateFormatter.dateFromString(string: "your old date as string here")

How can I convert including timezone date in swift?

Xcode 8 • Swift 3

You need to escape 'T' and use the appropriate timezone symbol. Note that it will depend if your date string represents a UTC (zero seconds from GMT) time timezone with Z "XXXXX" or without it +00:00 "xxxxx":

let dateString = "2015-08-14T20:02:25-04:00"
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssXXXXX"
if let date = formatter.date(from: dateString) {
formatter.dateFormat = "yyyy-MM-dd HH:mm"
let string = formatter.string(from: date)
print(string)
}

If you need some reference you can use this:

Sample Image

Getting wrong date when converting string with timezone

// This lets us parse a date from the server using the RFC3339 format
let rfc3339DateFormatter = DateFormatter()
rfc3339DateFormatter.locale = Locale(identifier: "en_US_POSIX")
rfc3339DateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
rfc3339DateFormatter.timeZone = TimeZone(secondsFromGMT: 0)

// This string is just a human readable format.
// The timezone at the end of this string does not mean your date
// will magically contain this timezone.
// It just tells the parser what timezone to use to convert this
// string into a date which is basically just seconds since epoch.
let string = "2019-01-14T00:00:00+08:00"

// At this point the date object has no timezone
let shiftDate = rfc3339DateFormatter.date(from: string)!

// If you want to keep printing in SGT, you have to give the formatter an SGT timezone.
let printFormatter = DateFormatter()
printFormatter.dateStyle = .none
printFormatter.timeStyle = .full
printFormatter.timeZone = TimeZone(abbreviation: "SGT")!
let formattedDate = printFormatter.string(from: shiftDate)

You will notice that it prints 12am. There is nothing wrong with your code. You just misunderstand the Date object. Most people do.

Edit: I used the RFC formatter found in the Apple docs here. The result is the same if you use your formatter. And yes, as rmatty said, there are a few things wrong with your formatter (I stand corrected :))

How to Convert date into local time zone ios swift]

If you want the result to be a Date object just use the first part of @Intellij-Shivam's answer:

func serverToLocal(date:String) -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
let localDate = dateFormatter.date(from: date)

return localDate
}

(note that DateFormatter.date(from:) returns an optional, which is correct because the input date string might not be in the correct format.)

There is no such thing as a Date in your local time zone. Dates don't have a time zone. They record an instant in time all over the planet.

To display a date in your local time zone you can use the DateFormatter class method localizedString():

let dateString = DateFormatter.localizedString(
inputDate,
dateStyle: .medium,
timeStyle: .medium)

Convert DateTime from one TimeZone to another TimeZone - Swift

Here is cleaned up version of your code with an example of how to run it, note that I select the "from" time zone that myTime has in this example

func changeToSystemTimeZone(_ date: Date, from: TimeZone, to: TimeZone = TimeZone.current) -> Date {
let sourceOffset = from.secondsFromGMT(for: date)
let destinationOffset = to.secondsFromGMT(for: date)
let timeInterval = TimeInterval(destinationOffset - sourceOffset)
return Date(timeInterval: timeInterval, since: date)
}

Example, note that you need to set a locale that matches the time zone for your input date (myTime)

let myTime = "2019-11-02 02:00:00"
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"

if let date = dateFormatter.date(from: myTime), let timeZone = TimeZone(abbreviation: "UTC") {
let offsetedDate = changeToSystemTimeZone(date, from: timeZone)
print(date, Calendar.current.timeZone)
print(offsetedDate, timeZone)
}

2019-11-02 01:00:00 +0000 Europe/Stockholm (current)

2019-11-02 02:00:00 +0000 GMT (fixed)

Update: Added a parameter for time zone to convert to

How to convert date from server in Swift?]

You need to learn a bit more about how to set the dateFormat string. You forgot the Z in the string. Also as a warning. Creating date formatter objects is extremely expensive. Do not create a new one for every date you want to format. Be sure to cache it and reuse it.

import Foundation

let serverDateFormatter:DateFormatter = {
let result = DateFormatter()
result.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SZ"
result.locale = Locale(identifier: "en_US_POSIX")
result.timeZone = TimeZone(secondsFromGMT: 0)
return result
}()

let date = "2021-09-08T20:52:47.001Z"
let dateformat = serverDateFormatter.date(from: date)

print(dateformat as Any)

How to convert string to date without change time swift

You can add a timezone to dateFormatter

dateFromat.timeZone = TimeZone(secondsFromGMT: 0)

Updated code:

let scheduleDate : String = "2020-01-25 20:11:00"
let dateFromat : DateFormatter = DateFormatter()
dateFromat.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFromat.timeZone = TimeZone.init(secondsFromGMT: 0)
let dateFromString = dateFromat.date(from: scheduleDate)
print(dateFromString as Any)


Related Topics



Leave a reply



Submit