Reputation:
I'm trying to restrict the time of a datePicker from 09:00 AM to 09:00 PM (21:00) with a time interval of 30 minutes. I have tried all known means for that and have been searching the internet for a long time. But I found nothing. Here is the code I wrote
//datePicker.setDate(dateFormatter.date(from: "09.00")!, animated: true)
datePicker.maximumDate = Calendar.current.date(byAdding: .hour, value: -12, to: dateFormatter.date(from: "21.00")!) //)Calendar.current.date(byAdding: Calendar.Component.hour, value: 1, to: Date())
datePicker.minimumDate = Calendar.current.date(byAdding: Calendar.Component.hour, value: -12, to: dateFormatter.date(from: "09.00")!)
But it didn't work. Please help.
Upvotes: 1
Views: 7620
Reputation: 1165
Restricting user so she/he can only pick date between 9 and 12 but will show all the available options as this is date picker view's default behaviour but by setting proper minimumDate and maximumDate user can only pick between 9 and 21. Try below code and let me know if it works
And if you want to display times of your choice I will suggest better create an array of times you want to display, So user will only choose from the array you will provide In this case times between 9 am and 9 pm like [9:00,9:30,10:00,10:30,.....,21:00] and set that array to normal picker
datePicker.datePickerMode = .time // setting mode to timer so user can only pick time as you want
datePicker.minuteInterval = 30 // with interval of 30
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
let min = dateFormatter.date(from: "9:00") //createing min time
let max = dateFormatter.date(from: "21:00") //creating max time
datePicker.minimumDate = min //setting min time to picker
datePicker.maximumDate = max //setting max time to picker
Upvotes: 9
Reputation: 93151
Try this:
let now = Date()
datePicker.datePickerMode = .time
datePicker.minimumDate = Calendar.current.date(bySettingHour: 9, minute: 0, second: 0, of: now)
datePicker.maximumDate = Calendar.current.date(bySettingHour: 21, minute: 0, second: 0, of: now)
datePicker.minuteInterval = 30
Upvotes: 0
Reputation: 1703
Did you try to achieve this from storyboard?
from code:
let calendar = Calendar.current
var dateComponents = calendar.dateComponents([.day, .month, .year], from: Date())
dateComponents.setValue(9, for: .hour)
let minDate = Calendar.current.date(from: dateComponents)
dateComponents.setValue(21, for: .hour)
let maxDate = Calendar.current.date(from: dateComponents)
self.datePicker?.minimumDate = minDate
self.datePicker?.maximumDate = maxDate
self.datePicker?.minuteInterval = 30
Upvotes: 0
Reputation: 1553
The values you want to put in minimumDate
and maximumDate
are 9am and 9pm of the current date. The current date is what is missing in your code
let cal = Calendar.current
let now = Date() // get the current date and time (2018-03-27 19:38:44)
let components = cal.dateComponents([.day, .month, .year], from: now) // extract the date components 28, 3, 2018
let today = cal.date(from: components)! // build another Date value just with date components, without the time (2018-03-27 00:00:00)
datePicker.minimumDate = today.addingTimeInterval(60 * 60 * 9) // adds 9h
datePicker.maximumDate = today.addingTimeInterval(60 * 60 * 21) // adds 21h
Upvotes: 2