Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting hours and minutes from UIDatePicker

I have a UIDatePicker set in time mode, and have a property referencing that date picker. I don't know how to extract hour and minutes components from UIDatePicker when set to time mode in Swift 3. Please help

like image 446
ar_shabazov Avatar asked Nov 29 '22 22:11

ar_shabazov


1 Answers

Even in .time mode the date property returns a Date instance.

Get hour and minute with Calendar and DateComponents

let picker = UIDatePicker()
picker.datePickerMode = .time

let date = picker.date
let components = Calendar.current.dateComponents([.hour, .minute], from: date)
let hour = components.hour!
let minute = components.minute!
like image 79
vadian Avatar answered Dec 05 '22 14:12

vadian