title: Fetching and setting date from datepicker, but still getting old default value
body: <p>I want to fetch and set date from <code>DatePicker</code>, but my date is not updating. SwiftUI is new to me and I am confused with what type of property wrapper to use. Please help in this and advice when and where to use <code>@State</code>, <code>@Binding</code>, <code>@Published</code> I read some articles but still concept is not clear to me.</p>
  
  <p>Here I used MVVM and SwiftUI and my code as follows.</p>
  
  <pre><code>class MyViewModel:ObservableObject {
      @Published var selectedDate : Date = Date()
      @Published var selectedDateStr : String = Date().convertDateToString(date: Date())
  
  }
  
  struct DatePickerView: View {
      @Environment(\.presentationMode) var presentationMode: Binding&lt;PresentationMode&gt;
      @ObservedObject var viewModel : MyViewModel
  
      var dateFormatter: DateFormatter {
          let formatter = DateFormatter()
          formatter.dateStyle = .long
          return formatter
      }
  
      @State private var selectedDate = Date()
  
      var body: some View {
          VStack {
              //Title
              HStack{
              Text("SELECT A DATE")
                  .foregroundColor(.white)
                  .font(.system(size: 20))
              }
                  .frame(width:UIScreen.main.bounds.width,height: 60)
              .background(Color.red)
  
              //Date Picker
              DatePicker(selection: $selectedDate, in: Date()-15...Date(), displayedComponents: .date) {
                  Text("")
              }.padding(30)
  
              Text("Date is \(selectedDate, formatter: dateFormatter)")
              Spacer()
  
              //Bottom buttons
  
                  Text("DONE")
                      .fontWeight(.semibold)
                      .frame(width:UIScreen.main.bounds.width/2,height: 60)
                      .onTapGesture {
                          self.viewModel.selectedDate = self.selectedDate
                          self.presentationMode.wrappedValue.dismiss()
  
                  }
          }
      }
  }
  
  //calling:
  
  DatePickerView(viewModel: self.viewModel)
  
  </code></pre>