In Swift 3, you can use DateComponentsthis as this path.
First add an action to datepickerfor events .valueChanged.
datepicker.addTarget(self, action: #selector(dateChanged(_:)), for: .valueChanged)
Then select the selected day, month, and year using DateComponentsin action.
func dateChanged(_ sender: UIDatePicker) {
let componenets = Calendar.current.dateComponents([.year, .month, .day], from: sender.date)
if let day = componenets.day, let month = componenets.month, let year = componenets.year {
print("\(day) \(month) \(year)")
}
}
source
share