3

I'm trying to get the data from the UIDatePicker, for example the user picked a date as 7:15 PM How do I get the data from that so I can manipulate and use it for some other function?

If you have any questions or need any clarifications please comment them down below.

alex
  • 1,746
  • 6
  • 19
  • 34

1 Answers1

18

Add a handler for your UIDatePicker like so:

yourDatePicker.addTarget(self, action: #selector(DatePickerViewController.handler(sender:)), for: UIControl.Event.valueChanged)

And then do something like this in your handler:

@objc func handler(sender: UIDatePicker) {
    let timeFormatter = DateFormatter()
    timeFormatter.timeStyle = DateFormatter.Style.short

    var strDate = timeFormatter.string(from: yourDatePicker.date)
    // do what you want to do with the string.
}
Community
  • 1
  • 1
Praneeth Wanigasekera
  • 946
  • 1
  • 10
  • 16