Unable to select future date from date picker view

285 views Asked by At

I am unable to select future dates from the date picker view. I would like to be able to select only current and future dates, not past dates.

{
    let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: EnterAccountCell2.self), for: indexPath) as! EnterAccountCell2

    cell.inputTextField.title = NSLocalizedString("StartDate", comment: "")
    cell.inputTextField.delegate = self
    cell.inputTextField?.placeholder = NSLocalizedString("PleaseSelect", comment: "")
    cell.inputTextField.titleFont = UIFont(name: UIConfiguration.getUIFONTAPP(), size: UIConfiguration.kFontSizeMedium)!
    cell.inputTextField.tag = 222
    cell.inputTextField.inputView = UIView(frame: .zero)
    cell.inputTextField.text = self.myDateShow
    cell.datePickerView.addTarget(self, action: #selector(datePickerChanged(picker:)), for: .valueChanged)
    cell.selectView.isHidden = false
    cell.inputTextField.isUserInteractionEnabled = true
    cell.datePickerView.minimumDate = Date()
    cell.datePickerView.maximumDate = Date(timeInterval: 10976*24*60*60, since: Date())
   
return cell

}
1

There are 1 answers

2
Aaron A On

To allow users to select only current and future dates, you can set the minimumDate property of the datePickerView to the current date, and the maximumDate property to the maximum date that you want to allow the user to select. For example, to allow users to select dates up to 30 days in the future, you can use the following code:

cell.datePickerView.minimumDate = Date()
cell.datePickerView.maximumDate = Calendar.current.date(byAdding: .day, value: 30, to: Date())

This code sets the minimumDate to the current date and the maximumDate to 30 days from the current date, so users will be able to select any date within the next 30 days but not any past dates. If you want to allow a different number of days into the future, you can adjust the value passed to the date(byAdding:value:to:) method accordingly.