1

I got an issue in trying to limit the date. With setting the date like that self.datePicker.maximumDate = [NSDate date]; it is only grey the date after now but enable the user to select a date after this maximum date.

So When the picker value changed I compare the selected date if up than the current date I edit it.

If I use both solutions I got weird behaviour, any idea ?

- (IBAction)pickerValueChanged:(id)sender {

    dispatch_async(dispatch_get_main_queue(), ^{
        UIDatePicker *datePicker = (UIDatePicker *)sender;

        if ([self.datePicker.date compare:[NSDate date]] == NSOrderedDescending) {

            datePicker.date = [NSDate date];
        }

    });
}

This function is triggered when the date value from the date picker did change. If I set a minimum and a maximum date I got a weird behaviour. Any idea?

EDIT:

Adrien G
  • 228
  • 1
  • 4
  • 12

5 Answers5

4

Either you can set the maximum date in xcode or programatically.

1.Programatically you can set the picker maximum date by using the below line of code.

[self.yourPickerView setMaximumDate: [NSDate date]];

This line of code will set the current date as maximum date that can select in picker.

Rahul K Rajan
  • 776
  • 10
  • 19
  • Not sure I understand your first comment, I understand it up to "...meaning fully." What does this bit mean? Also what value does this add over the over the other answers? Surely the other answers have provided what your answer says already? – Popeye Jan 05 '16 at 09:11
  • Forget the last part of my previous comment, the fact yours has a description of whats going on adds value as the others don't have anything. +1 just for that. – Popeye Jan 05 '16 at 09:16
  • I'm actually shocked someone can take a little bit of criticism on here, normally you leave a slightly negative comment which is meant to be a little bit of helpful feedback and it's like you've just killed there mother or something. Good Coding. – Popeye Jan 05 '16 at 09:21
2

Yes, you can set minimum and maximum date, using .xib and picker settings

enter image description here

Arthur
  • 1,740
  • 3
  • 16
  • 36
1

Yes you can set Maximum date and minimum date to UIDatePickerView

Below is the another stackover flow link

UIDatePicker, setting maximum and minimum dates based on todays date

Community
  • 1
  • 1
Priyank Gandhi
  • 1,257
  • 1
  • 16
  • 29
-1
- (IBAction) datePickerChanged:(id)sender {
// When `setDate:` is called, if the passed date argument exactly matches the Picker's date property's value, the Picker will do nothing. So, offset the passed date argument by one second, ensuring the Picker scrolls every time.
NSDate* oneSecondAfterPickersDate = [picker.date dateByAddingTimeInterval:1] ;
if ( [picker.date compare:picker.minimumDate] == NSOrderedSame ) {
    NSLog(@"date is at or below the minimum") ;
    picker.date = oneSecondAfterPickersDate ;
}
else if ( [picker.date compare:picker.maximumDate] == NSOrderedSame ) {
    NSLog(@"date is at or above the maximum") ;
    picker.date = oneSecondAfterPickersDate ;
    }
}
codercat
  • 22,873
  • 9
  • 61
  • 85
Gates
  • 67
  • 6
  • I hate answers that don't provide a description of what it is doing so I'm giving this -1. The reason behind why answers should include a description is because if a newbie was looking for an answer they might not understand what a post with just code in it actually does. – Popeye Jan 05 '16 at 09:14
-1
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *currentDate = [NSDate date];
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:currentDate];
NSInteger year = [components year];

NSDateFormatter* formatter1 = [[NSDateFormatter alloc] init];
[formatter1 setDateFormat:@"yyyy-MM-dd"];
NSString *dateString = [NSString stringWithFormat:@"%ld-12-31",(long)year];
NSDate *maxDate = [formatter1 dateFromString:dateString];
[self.dobPickerView setMaximumDate: maxDate];
Nimmy
  • 89
  • 6
  • I hate answers that don't provide a description of what it is doing so I'm giving this -1. The reason behind why answers should include a description is because if a newbie was looking for an answer they might not understand what a post with just code in it actually does. – Popeye Jan 05 '16 at 09:14