# String Date to Date

**URL:** <https://forums.swift.org/t/string-date-to-date/62023>\
**Category:** Using Swift\
**Tags:** date, time\
**Created:** [December 13, 2022, 11:41pm UTC](https://forums.swift.org/t/string-date-to-date/62023 "2022-12-13T23:41:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashleydee1999](https://avatars.discourse-cdn.com/v4/letter/a/e36b37/32.png) [@ashleydee1999](https://forums.swift.org/u/ashleydee1999)\
**Post date:** [December 13, 2022, 11:41pm UTC](https://forums.swift.org/t/string-date-to-date/62023/1 "2022-12-13T23:41:20Z")

</div>

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

```swift
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!

---

<div class="post-metadata">

**Author:** ![AlexanderM](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alexanderm/32/9781_2.png) [@AlexanderM](https://forums.swift.org/u/AlexanderM)\
**Post date:** [December 14, 2022, 12:38am UTC](https://forums.swift.org/t/string-date-to-date/62023/2 "2022-12-14T00:38:45Z")

</div>

Dates don't know about time zones. It's useful to know that a `Date` object is really just a wrapper around a `Double` number of seconds after some reference date. Clearly, such a number wouldn't know anything about time zones.

> **fun little demo**
>
> Obviously, don't actually rely on this implementation detail, but it's neat to see:
> 
> ```swift
> import Foundation
> 
> let d = Date()
> 
> // The exact number will depend on when you run this code, but it'll give the same result
> print(d.timeIntervalSinceReferenceDate) // => 692674495.061181
> print(unsafeBitCast(d, to: Double.self)) // => 692674495.061181
> 
> ```

The timezone is a layer of interpretation of this number, imposed by the presentation layer (in this case, `print`, which always prints a timestamp in your local timezone).

See [Date in Swift printed is wrong | Apple Developer Forums](https://developer.apple.com/forums/thread/689720)

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [December 14, 2022, 1:07am UTC](https://forums.swift.org/t/string-date-to-date/62023/3 "2022-12-14T01:07:30Z")

</div>

From apple docs:

> [@](#):
>
> ### Working With Fixed Format Date Representations
> 
> ...  
> When working with fixed format dates, such as RFC 3339, you set the [`dateFormat`](https://developer.apple.com/documentation/foundation/dateformatter/1413514-dateformat) property to specify a format string. For most fixed formats, you should also set the [`locale`](https://developer.apple.com/documentation/foundation/dateformatter/1411973-locale) property to a POSIX locale (`"en_US_POSIX"` ), and set the [`timeZone`](https://developer.apple.com/documentation/foundation/dateformatter/1411406-timezone) property to UTC.

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

let fixedFormatter = DateFormatter()
fixedFormatter.locale = Locale(identifier: "en_US_POSIX")
fixedFormatter.timeZone = TimeZone(secondsFromGMT: 0)
fixedFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"

let date = fixedFormatter.date(from: isoDate)!

let displayFormatter = DateFormatter()
displayFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
// uses current locale and current time zone
// overriding those, for test
displayFormatter.locale = Locale(identifier: "en_US_POSIX")
displayFormatter.timeZone = TimeZone(secondsFromGMT: (5*60+30)*60)
let dateString = displayFormatter.string(from: date)

print("date: \(date)") // 2022-12-13 23:00:00 +0000
print("dateString: \(dateString)") // 2022-12-14T04:30:00GMT+05:30

```

Note:

> [@](#):
>
> Important
> 
> In macOS 10.12 and later or iOS 10 and later, use the [`ISO8601DateFormatter`](https://developer.apple.com/documentation/foundation/iso8601dateformatter) class when working with ISO 8601 date representations.

The reason for this warning is unknown to me but I would not ignore it.

Also there are a newer `FormatStyle` and `ParseableFormatStyle` (macOS 12 and later, iOS 15 and later)
