Home > Blockchain >  datepicker formatting on displaying date
datepicker formatting on displaying date

Time:09-26

Hello after updating to Xcode 14 I'm facing with an issue on date picker so when I pick some date sometime date would be formatted like: 25 sep 2022 other times 30/09/2022 in Xcode 13 it was working fine

DatePicker("transactionDate", selection: $vm.transactionDate, displayedComponents: [.date])
.labelsHidden()
.id(vm.transactionDate)
.onChange(of: vm.transactionDate) { date in
 vm.selectedEndRecurrenceDate = date
}
}
.datePickerStyle(CompactDatePickerStyle())

So I don't understand now how to fix, nothing is changed in the code

CodePudding user response:

Adding an .id() modifier to your picker was a workaround for an old bug, see this question

When testing this in Xcode 14.1 beta (iOS) in the simulator I can reproduce the issue when I have the id modifier but as soon as I remove it the picker works fine

So it seems the hack to workaround the previous bug now creates a bug! So simply remove the .id() modifier since it isn't useful here.

DatePicker("transactionDate", selection: $vm.transactionDate, displayedComponents: [.date])
    .labelsHidden()
    .onChange(of: vm.transactionDate) { date in
        vm.selectedEndRecurrenceDate = date
    }
}
.datePickerStyle(CompactDatePickerStyle())

CodePudding user response:

Here is some test code that shows the date is printed/displayed as expected.

struct ContentView: View {
    @State var transactionDate = Date()
    
    var body: some View {
        VStack (spacing: 55){
            Text(transactionDate.formatted())
            DatePicker("transactionDate", selection: $transactionDate, displayedComponents: [.date])
                .labelsHidden()
                // no id here <---
                .onChange(of: transactionDate) { date in
                    print("---> transactionDate: \(transactionDate)")
                }
                .datePickerStyle(CompactDatePickerStyle())
        }
    }
}
  • Related