iOS Swift 3 - UIDatePicker

In Swift 3 you can use DateComponents for that like this way.

First add action on datepicker for .valueChanged events.

datepicker.addTarget(self, action: #selector(dateChanged(_:)), for: .valueChanged)

Then get selected day, month and year using DateComponents in the action.

func dateChanged(_ sender: UIDatePicker) {
    let components = Calendar.current.dateComponents([.year, .month, .day], from: sender.date)
    if let day = components.day, let month = components.month, let year = components.year {
        print("\(day) \(month) \(year)")
    }
}

UIDatePicker in Swift 3 and Swift 4 example.

In Swift 3 You can use

  1. Create Simple Project of Single View Application in swift 3
  2. Drag a text field & connect to viewController
  3. Date picker look like image

enter image description here

  1. ViewController code is bellow..

      class ViewController: UIViewController {
         //Text Field Connection
       @IBOutlet weak var txtDatePicker: UITextField!
       //Uidate picker
       let datePicker = UIDatePicker()
    
        override func viewDidLoad() {
        super.viewDidLoad()
       //show date picker
         showDatePicker()
      }
    
    
       func showDatePicker(){
       //Formate Date
        datePicker.datePickerMode = .date
    
        //ToolBar
       let toolbar = UIToolbar();
       toolbar.sizeToFit()
    
       //done button & cancel button
        let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.bordered, target: self, action: "donedatePicker")
        let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
       let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.bordered, target: self, action: "cancelDatePicker")
       toolbar.setItems([doneButton,spaceButton,cancelButton], animated: false)
    
    // add toolbar to textField
    txtDatePicker.inputAccessoryView = toolbar
     // add datepicker to textField
    txtDatePicker.inputView = datePicker
    
       }
    
     func donedatePicker(){
      //For date formate
       let formatter = DateFormatter()
       formatter.dateFormat = "dd/MM/yyyy"
       txtDatePicker.text = formatter.string(from: datePicker.date)
       //dismiss date picker dialog
       self.view.endEditing(true)
        }
    
          func cancelDatePicker(){
           //cancel button dismiss datepicker dialog
            self.view.endEditing(true)
          }
       }
    
  2. Full Source project is given to github. github Link:https://github.com/enamul95/DatePicker1

    In Swift 4 or 5 you can use bellow code

     class ViewController: UIViewController {
    
       @IBOutlet weak var txtDatePicker: UITextField!
      let datePicker = UIDatePicker()
    
      override func viewDidLoad() {
        super.viewDidLoad()
         showDatePicker()
      }
    
    
      func showDatePicker(){
        //Formate Date
        datePicker.datePickerMode = .date
    
       //ToolBar
       let toolbar = UIToolbar();
       toolbar.sizeToFit()
       let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(donedatePicker));
       let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
      let cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancelDatePicker));
    
     toolbar.setItems([doneButton,spaceButton,cancelButton], animated: false)
    
      txtDatePicker.inputAccessoryView = toolbar
      txtDatePicker.inputView = datePicker
    
     }
    
      @objc func donedatePicker(){
    
       let formatter = DateFormatter()
       formatter.dateFormat = "dd/MM/yyyy"
       txtDatePicker.text = formatter.string(from: datePicker.date)
       self.view.endEditing(true)
     }
    
     @objc func cancelDatePicker(){
        self.view.endEditing(true)
      }
    }