Eric33187
Eric33187

Reputation: 1156

How to get hours and minutes from UIDatePicker

How do you get the hours: Int and minutes: Int from a UIDatePicker?

I know it has a .date property, but I'm finding it difficult to divulge hour and minute values directly. Is there an easier way than doing the 'hacky-ish' way I have in mind, which is to compare the current date to the datePicker's date, then turn that time interval into hours and minutes and add it to the current hours and minutes. Seems overly complex for a seemingly ordinary task...

Upvotes: 0

Views: 1036

Answers (1)

Itachi
Itachi

Reputation: 6070

UIDatePicker just give you the selected Date, if you need the date component based on current calendar, check out the Calendar and DateComponent documendation for details.

let now = Date() // your date
let dateComponents = Calendar.current.dateComponents([.hour, .minute], from: now)

dateComponents.hour
dateComponents.minute

If you need hours and minutes for time distance, you need the calculate it manually with your prefer base date.

Upvotes: 3

Related Questions