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?
A control for inputting date and time values.
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.
UIDatePicker in Swift 3 and Swift 4 example.
In Swift 3 You can use
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) } }
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) } }
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)") } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With