Set time zones with the new date formatting API in iOS 15

I would like to update some code that uses DateFormatter, for example, code like this:

let date = Date()
let formatter = DateFormatter()
formatter.dateStyle = .medium
formatter.timeStyle = .short
formatter.timeZone = TimeZone(identifier: "Europe/London")
formatter.string(from: date)

With the new date formatting API available in iOS 15, date formatting can be achieved by calling the formatted() method on the date directly, without explicitly requiring a DateFormatter instance, for example:

date.formatted(.dateTime.year().month().day().hour().minute())

However, using this new API, I cannot find any way to set the time zone, i.e., the equivalent of the line formatter.timeZone = TimeZone(identifier: "Europe/London").

I'm aware that time zone can be used to format the output, for example date.formatted(.dateTime.timeZone()), but this does not allow one to actually set the time zone.

Does anyone know if it is possible?

Answered by DTS Engineer in 704246022

To do this, change the timeZone property on the style itself. For example, this code:

import Foundation

func main() {
    let d = Date()
    var f = Date.FormatStyle.dateTime
    print(d.formatted(f))
    f.timeZone = TimeZone(identifier: "Australia/Perth")!
    print(d.formatted(f))
}

main()

prints:

09/02/2022, 10:04
09/02/2022, 18:04

[I’m on GMT so I can’t use your Europe/London example (-: ]

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

To do this, change the timeZone property on the style itself. For example, this code:

import Foundation

func main() {
    let d = Date()
    var f = Date.FormatStyle.dateTime
    print(d.formatted(f))
    f.timeZone = TimeZone(identifier: "Australia/Perth")!
    print(d.formatted(f))
}

main()

prints:

09/02/2022, 10:04
09/02/2022, 18:04

[I’m on GMT so I can’t use your Europe/London example (-: ]

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Hi Quinn :)

Great to see an example of how you have save a FormatStyle in variable! I really like the new format but it is sometimes really hard to use it without concrete examples...

So what if I would like to have date.formatted(date: .long, time: .standard) but with TimeZone(secondsFromGMT: 0)?

Is that possible to combine those two easily?

Cheers!

The trick here is to initialise the format style with the date and time styles you want:

let d = Date()
var f = Date.FormatStyle(date: .long, time: .standard)
print(d.formatted(f))
f.timeZone = TimeZone(secondsFromGMT: 0)!
print(d.formatted(f))

This prints:

3 May 2022, 10:24:26
3 May 2022, 9:24:26

And hey hey, I’m on to BST so I can use GMT this time around.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Of course! Thanks a lot for this Quinn! 🤓👍

Set time zones with the new date formatting API in iOS 15
 
 
Q