Home > other >  How to change a string I get from API
How to change a string I get from API

Time:04-08

I get a string with a date of the first flight for each of four rockets from this API. The string that I get looks like this: "2006-03-24". I want it to look this way: "24 March, 2006". I don't understand how to do this in SwiftUI.

My View with the dates:

struct DateView: View {
    @State var rockets = [RocketInfo]()
    
    var body: some View {
        NavigationView {
            if rockets.isEmpty {
                ProgressView()
            } else {
                TabView {
                    ForEach(rockets) { rocket in
                        VStack {
                            HStack {
                                Text("First flight")
                                Spacer()
                                Text(rocket.firstFlight)
                            }
                            .padding()
                        }
                    }
                }
                .tabViewStyle(.page)
                .navigationBarTitleDisplayMode(.inline)
                .navigationBarHidden(true)
            }
        }
        .onAppear {
            InfoApi().getRockets { rockets in
                self.rockets = rockets
            }
        }
    }
}

struct DateView_Previews: PreviewProvider {
    static var previews: some View {
        DateView()
            .preferredColorScheme(.dark)
    }
}

Screenshot of the API data: enter image description here

CodePudding user response:

You'll need two instances of DateFormatter - one to convert the string from API to a Date instance and the other to convert the date instance to your desired string format.

let dateFormatter1 = DateFormatter() 
dateFormatter1.dateFormat = "yyyy-MM-dd" // Format of API Date

let dateFormatter2 = DateFormatter() 
dateFormatter2.dateFormat = "d MMMM, yyyy" // Desired Format

let date = dateFormatter1.date(from: "2006-03-24")!
let desiredFormat = dateFormatter2.string(from: date)
print(desiredFormat) // prints "24 March, 2006"

CodePudding user response:

The func gets a date from the string, which you then can format to your wishes.

    var body: some View {
        let dateString = "2006-03-24"
        let date = dateFromString(dateString)
        
        Text("\(date, style: .date)")
    }
    
    func dateFromString(_ string: String) -> Date {
        let dateFormatter = DateFormatter()
        dateFormatter.locale = .current
        dateFormatter.dateFormat = "yyyy-MM-dd"
        return dateFormatter.date(from: string) ?? Date()
    }

CodePudding user response:

First, I encourage you to change firstFlight in RocketInfo to be a Date object, to get the correct data representation in the model. You may have to configure your JSONDecoder to parse the date string correctly:

let decoder = JSONDecoder()
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
decoder.dateDecodingStrategy = .formatted(formatter)

Then, you need another DateFormatter when you want to convert that date back into a string:

let formatter = DateFormatter()
formatter.dateFormat = "d MMMM, yyyy"
let dateString = formatter.string(from: object.firstFlight)

Please see here for the list of custom formats supported by DateFormatter: http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns

(There's also a couple of system defined formats, please see dateStyle and timeStyle in the docs.)

  • Related