Bhanuteja
Bhanuteja

Reputation: 801

How to set restriction for timePicker

Here am using timePicker through UIDatePickerMode.time

enter image description here

Now, here my current time is 3:13 PM, but in time picker I want to show 30 minutes from now I.e., 3:43 PM
 that means user can select time from 3: 43 PM onwards only. 
how should I achieve this ?

This is the code which I tried I tried by giving timePicker?.minimumDate. But it didn't work

    func timePicking(){
                    let timePicker = ActionSheetDatePicker(title: "Time", datePickerMode: UIDatePickerMode.time, selectedDate: NSDate() as Date!, target: self, action: #selector(self.timeSelected), origin: (sender as AnyObject).superview!?.superview)
                    let minimumTimeRestrict: TimeInterval = 0 * 30 * 60 * 60
                    timePicker?.minimumDate = NSDate(timeInterval: -minimumTimeRestrict, since: NSDate() as Date) as Date!
                    timePicker?.show()
}

Upvotes: 0

Views: 3172

Answers (1)

Jaydeep Vora
Jaydeep Vora

Reputation: 6213

Swift 4.0

If You want to display time 30 minute after in UIDatePicker, Add minimumDate as like below.

let nextTime = Calendar.current.date(byAdding: .minute, value: 30, to: Date())            
timePicker.minimumDate = nextTime

Upvotes: 2

Related Questions