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

SOLVED: How do I change the TimeZone of DateFormatter dynamically while displaying a List?

Forums > SwiftUI

I'm trying to display a list of different times that could have different time zones. I have created a Model that includes an ID = UUID(), name: String, time: Date, and localTimeZone: TimeZone.

Ideally, I'd like to either create a new or change the timezone of an existing dateformatter for every item in the list. Here is the framework of code I am trying to get working:

Thank you, I appreicate any tips and suggestions.

import SwiftUI

struct ExampleModel: Identifiable {
    var id = UUID()
    var name: String
    var date: Date
    var localTimeZone: TimeZone
}

let sampleData = [

    ExampleModel(name: "Name 1", date: dateFormatter.date(from: "02:00L")!, localTimeZone: TimeZone(abbreviation: "PST")!),
    ExampleModel(name: "Name 2", date: dateFormatter.date(from: "05:17L")!, localTimeZone: TimeZone(abbreviation: "EST")!),
    ExampleModel(name: "Name 3", date: dateFormatter.date(from: "18:33L")!, localTimeZone: TimeZone(abbreviation: "GMT")!)
]

let dateFormatter: DateFormatter = {
    let formatter = DateFormatter()
    formatter.dateFormat = "HH':'mm'L'"
    formatter.timeZone = TimeZone(secondsFromGMT: 0)
    return formatter
}()

struct DateFormatterTest: View {
    var exampleData: [ExampleModel]

    var body: some View {
        List(sampleData) { item in
            HStack {
                Text(item.name)
                //dateFormatter.timeZone = item.localTimeZone------------//
                Text(dateFormatter.string(from: item.date)) //Display this in local time.
            }
        }
    }
}

struct DateFormatterTest_Previews: PreviewProvider {
    static var previews: some View {
        DateFormatterTest(exampleData: sampleData)
    }
}

Thanks again, Jacob

3      

Got it!

I created a 'func' to run the code I needed to create a DateFormatter and return a String, then called it in the view. So simple, but for a new guy it took a while.

Thanks to this video for giving me the idea: https://www.hackingwithswift.com/books/ios-swiftui/calculating-the-total-per-person

I'll be making some final tweeks for my actual project, here's how I made it work:

...
let dateFormatter: DateFormatter = {
    let formatter = DateFormatter()
    formatter.dateFormat = "HH':'mm'Z'"
    formatter.timeZone = TimeZone(secondsFromGMT: 0)
    return formatter
}()

struct DateFormatterTest: View {
    var exampleData: [ExampleModel]

    var body: some View {
        List(sampleData) { item in
            HStack {
                Text(item.name)
                //dateFormatter.timeZone = item.localTimeZone------------//
                Text(dateFormatter.string(from: item.date)) //Display this in local time.
                Text(getTimeFormatted(time: item.date, timeZone: item.localTimeZone))
            }
        }
    }
}

func getTimeFormatted (time: Date, timeZone: TimeZone) -> String {
    let timeFormat = DateFormatter()
    timeFormat.dateFormat = "HH':'mm'L'"
    timeFormat.timeZone = timeZone
    return timeFormat.string(from: time)
}
...

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.