4 Answers
You can set the minimum and maximum date properties of your UIDatePicker view.
You can create the dates using NSDateComponents, and just set the Hour and Minute components.
This won't show a popup, but the user will not be able to select an invalid date. (Which is the recommended behavior).
To check if the date in the input field is valid, you can use the NSDate comparison methods agains your min and max dates.

- 564
- 1
- 5
- 18
u can use this function to check selected date (date) is in range (begindate - enddate)
- (BOOL)date:(NSDate*)date isBetweenDate:(NSDate*)beginDate andDate:(NSDate*)endDate
{
return ([date compare:beginDate] == NSOrderedAscending) && ([date compare:endDate] == NSOrderedDescending)
}
for Swift :
extension NSDate
{
func isBetweenDates(beginDate: NSDate, endDate: NSDate) -> Bool
{
return self.compare(beginDate) == .OrderedAscending && self.compare(endDate) == .OrderedDescending
}
}

- 12,352
- 9
- 44
- 63

- 889
- 9
- 21
-
See http://stackoverflow.com/questions/1072848/how-to-check-if-an-nsdate-occurs-between-two-other-nsdates for more info – Pandafox Jan 25 '16 at 09:08
-
do you know it in swift? – danialrayn Jan 25 '16 at 09:29
-
The link I posted also contains an example for swift – Pandafox Jan 25 '16 at 09:53
You can follow the suggestion in Pandafox's answer, set minimumDate
and maximumDate
of your UIDatePicker
. Or answer your question
As two other answer's shown are not given in Swift, I will try with the following.
First, you need to set your picker to listen the control event
// Add this in viewDidLoad
datePicker.addTarget(self, action: "changedAction", forControlEvents: UIControlEvents.ValueChanged)
Then in the action, get the value, check within range, and show alert if needed.
func changedAction() {
var datePicked = datePicker.date
if datePicked.isGreaterThanDate(endDate) && datePicked.isLessThanDate(startDate) {
// show alert
let alertController = UIAlertController(title: "Warning", message: "Date picked out of range", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default,handler: nil))
self.presentViewController(alertController, animated: true, completion: nil)
}
}
Use extension methods to compare dates, check more here
extension NSDate {
func isGreaterThanDate(dateToCompare: NSDate) -> Bool {
return self.compare(dateToCompare) == NSComparisonResult.OrderedDescending
}
func isLessThanDate(dateToCompare: NSDate) -> Bool {
return self.compare(dateToCompare) == NSComparisonResult.OrderedAscending
}
you can check like this
- (void)viewDidLoad
{
[super viewDidLoad];
UIDatePicker* picker = [[UIDatePicker alloc]initWithFrame:self.view.bounds];
[self.view addSubview:picker];
picker.datePickerMode = UIDatePickerModeTime;
[picker addTarget:self action:@selector(pickerAction:) forControlEvents:UIControlEventValueChanged];
}
- (void)pickerAction:(UIDatePicker*)picker
{
// do something such as check if the picker.data is valid
NSLog(@"pickerAction %@",picker.date);
}

- 1