UIDatePicker setting minimum and maximum hour

前端 未结 4 623
孤独总比滥情好
孤独总比滥情好 2021-01-17 03:07

How to set minimum and maximum hour to UIDatePicker ? At all is it possible ?

I know how to set minimum and maximum date:

[datePicker setMinimumDate: today];

4条回答
  •  清酒与你
    2021-01-17 03:21

    This works in "TIME MODE", otherwise selected answer should work unintuitively though

        // minimum time
        components.hour = 0
        components.minute = 0
        let minTime = calendar.date(from: components)
    
        // max time
        components.hour = 23
        components.minute = 59
        let maxTime = calendar.date(from: components)
    
        // selected value current time or saved time
        let selectedDate = Date() // or saved time
        let nowComp = calendar.dateComponents([.hour, .minute], from: selectedDate)
        components.hour = nowComp.hour
        components.minute = nowComp.minute
        let selectedTime = calendar.date(from: components)!
    

    play with the values and use as below

        datePicker.datePickerMode = .time
        datePicker.date = selectedTime 
        datePicker.minimumDate = minTime 
        datePicker.maximumDate = maxTime 
    

提交回复
热议问题