Disble Past Date in UIDatePicker MinimumDate and MaximumDate


I have problem, i want to make UIdatePiker just use present time and future time
you need this code in your date picker

  let celendar: NSCalendar = NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian)!
    let currentDate: NSDate = NSDate()
    let components: NSDateComponents = NSDateComponents()

and in viewDid load :

  components.day = 0
        let minDate: NSDate = celendar.date(byAdding: components as DateComponents,   to: currentDate as Date, options: NSCalendar.Options(rawValue: 0))! as NSDate
        
        components.month = 12
        let maxDate: NSDate = celendar.date(byAdding: components as DateComponents, to: currentDate as Date, options: NSCalendar.Options(rawValue: 0))! as NSDate
        
        self.picker.minimumDate = minDate as Date
        self.picker.maximumDate = maxDate as Date


and this full code .Swift


import UIKit

class GenderViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet var textDate: UITextField!
    let picker = UIDatePicker()
    
    let celendar: NSCalendar = NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian)!
    let currentDate: NSDate = NSDate()
    let components: NSDateComponents = NSDateComponents()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        datePickerValueChanged()
        
        let toolbar: UIToolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: 30))
        let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
        let doneBtn: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(doneButtonAction))
        toolbar.setItems([flexSpace, doneBtn], animated: false)
        toolbar.sizeToFit()
        
        components.day = 0
        let minDate: NSDate = celendar.date(byAdding: components as DateComponents,   to: currentDate as Date, options: NSCalendar.Options(rawValue: 0))! as NSDate
        
        components.month = 12
        let maxDate: NSDate = celendar.date(byAdding: components as DateComponents, to: currentDate as Date, options: NSCalendar.Options(rawValue: 0))! as NSDate
        
        self.picker.minimumDate = minDate as Date
        self.picker.maximumDate = maxDate as Date
    }
    
    @objc func doneButtonAction() {
        view.endEditing(true)
    }
    
    func datePickerValueChanged() {
        let loc = Locale(identifier: "id")
        let toolBar = UIToolbar()
        toolBar.sizeToFit()
        
        TextTanggal.inputAccessoryView = toolBar
        TextTanggal.inputView = picker
        picker.datePickerMode = .date
        picker.locale = loc
        
        let done = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(donePressed))
        toolBar.setItems([done], animated: true)
    }
    
    @objc func donePressed() {
        let format = DateFormatter()
        format.dateStyle = .medium
        format.timeStyle = .none
        let dateString = format.string(from: picker.date)
        
        TextTanggal.text = "\(dateString)"
        view.endEditing(true)
        format.dateFormat = "dd MMMM yyyy"
        TextTanggal.text! = format.string(from: picker.date)
        
        format.dateFormat = "yyyy-M-d"
        let newDate = format.string(from: picker.date)
    }
}









No comments: