I want to set DatePicker from a String to a certain date when it appears.
@State var staff: Staff
DatePicker(selection: $staff.birthDate, in: ...Date(),displayedComponents: .date) {
Text("Birth Date:")
}
What I expect is something like that, but it didn't work because selection requires Binding<Date> and $staff.birthDate is a String. How do I convert it?
I tried to create a func to format it like so:
func formatStringToDate(date: String?) -> Binding<Date> {
@Binding var bindingDate: Date
let dateForm = DateFormatter()
dateForm.dateFormat = "dd-MM-YYYY"
// _bindingDate = date
return dateForm.date(from: date ?? "")
}
But it still doesn't work. Any idea on how to solve this? Or did I approach this wrong?
Thankyou in advance.