Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Swift 3 - UIDatePicker

I have a UIDatePicker that shows the day, month and the year. But I don't know how to get those parameters.

So What is the best way to get the day, month and year from UIDatePicker using Swift 3?

like image 364
Anthony Shahine Avatar asked Nov 08 '16 10:11

Anthony Shahine


People also ask

What is UIDatePicker?

A control for inputting date and time values.

What is Datepicker in Swift?

DatePicker is a control used in iOS applications to get the date and time values from the user. We can allow the user to either enter the time in point or time interval. class UIDatePicker : UIControl.


2 Answers

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)   } } 
like image 104
Enamul Haque Avatar answered Sep 19 '22 19:09

Enamul Haque


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)")     } } 
like image 35
Nirav D Avatar answered Sep 18 '22 19:09

Nirav D