I am using this code to block the user from going over the limit I set:
in view did load:
NSDate *Date=[NSDate date];
[DatePickerForDate setMinimumDate:Date];
[DatePickerForDate setMaximumDate:[Date dateByAddingTimeInterval: 63072000]]; //time interval in seconds
And this method:
- (IBAction)datePickerChanged:(id)sender{
if ( [DatePickerForDate.date timeIntervalSinceNow ] < 0 ){
NSDate *Date=[NSDate date];
DatePickerForDate.date = Date;
}
if ( [DatePickerForDate.date timeIntervalSinceNow ] > 63072000){
NSDate *Date=[NSDate date];
DatePickerForDate.date = Date;
}
}
The first part works (the one with <0), and returns to the current date, but the one > 63072000, sometimes works and sometimes doesn’t. By the way 63072000 is about 2 years. Any ideas?
I experimented using a UIDatePicker with a maximumDate of one month from now:
I found that:
dateproperty will return the nearest date that’s in range.setDate:orsetDate:animated:, if the date you pass is the exact same date returned by the Picker’sdateproperty, the Picker will do nothing.With that in mind, here is a method that you can call when the Picker’s value changes that prevents you from ever selecting a date out of range:
The above
ifandelse ifsections are nearly identical, but I kept them separate so that I could see the different NSLogs, and also to better debug.Here’s the working project on GitHub.