UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

String Date to Date

Forums > Swift

Hi, I'm trying to convert my String Date to a Date, but it's displaying the wrong time

let isoDate = "2022-12-14T04:30:00+05:30"

let currentDateFormatter = DateFormatter()
currentDateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let initDate = currentDateFormatter.date(from:isoDate)!
print("initDate: \(initDate)") // prints initDate: 2022-12-13 23:00:00 +0000

The timezone in the string date is GMT+5:30 so I'd expect my date to also be the same, however it comes back as GMT.

From the code above isoDate != initDate

What am I misunderstanding?

Thanx in advance

2      

The Apple reference for DateFormatter says to use ISO8601DateFormatter instead when the string representation is in ISO date format.

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.