SwiftUI Date Picker is Problematic

I am using the swiftUI DatePicker like so:

Code Block swift
DatePicker("Start Date", selection: $profile.goalDate, in: dateRange, displayedComponents: [.date])


Basically, following the tutorial on the apple website. However, the DatePicker echoes out this error in the console:

Code Block
[DatePicker] UIDatePicker 0x7fe42bc36e90 is being laid out below its minimum width of 280. This may not look like expected, especially with larger than normal font sizes.


And when I select certain dates, the picker format changes abruptly.

So, the start date might be "Mar 2, 2021". Then, when I use the picker to select let's say 31st March, the portion showing the selected date moves to the right, and the format changes to "31/3/21". This is not how it is supposed to behave

Initial Behavior (Expected) imgur.com/GI38I7A
Behavior after selecting a date (Unexpected) imgur.com/vuZjhaj

I think it is linked to the locale.
Did you try to set locale (see https://stackoverflow.com/questions/58610557/swiftui-datepicker-displaying-language-in-english-only):
Code Block
DatePicker(…)
.environment(\.locale, Locale.init(identifier: "us")). // Or the locale you want

Have a look at the solution presented here :
https://stackoverflow.com/questions/59051670/change-selected-date-format-from-datepicker-swiftui
Have you found a usable solution to this yet?

I got the same problem and was not able to fix these issues yet.
The date format is getting changed with specific dates and not everytime i select a new one.

BUT, I found a workaround.

You can build the DatePicker yourself and just display a text with the formatted date from the DatePicker. When pressing on it, show the DatePicker.

I hope this gets fixed eventually.

I had the same problem. (Swift 5.x, Xcode 12.5.1) I selected the Date Picker, then changed the "Preferred Style" in the Attributes Inspector from "Automatic" to "Wheels." That fixed it for me.

SwiftUI Date Picker is Problematic
 
 
Q