IPhone UIDatePicker setMaximumDate

I am trying to create a datepicker that has today as the minimum date and February 1, 2011 as the maximum date.

I set the minimum date as follows

[picker setMinimumDate: [NSDate date]];

and this works fine, but MaximumDate doesn't seem to be correct.

[picker setMaximumDate: [NSDate dateWithNaturalLanguageString:@"11/02/01"]];

How to set the maximum date?

+3
source share
3 answers

Found the answer. Thanks anyway for your help.

[picker setDatePickerMode:UIDatePickerModeDate];

NSDateFormatter* formatter1 = [[NSDateFormatter alloc] init];
[formatter1 setLenient:YES];
[formatter1 setDateStyle:NSDateFormatterShortStyle];
[formatter1 setTimeStyle:NSDateFormatterShortStyle];

NSString* dateString = @"February 1 2011 10:00am";
NSDate* maxDate = [formatter1 dateFromString:dateString];

[picker setMinimumDate: [NSDate date]];
[picker setMaximumDate: maxDate];
+8
source
 NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
        NSDate *currentDate = [NSDate date];
        NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];
        [comps setYear:0];
        NSDate *maxDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
        [comps setYear:-5];
        NSDate *minDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];

        [datePicker setMaximumDate:maxDate];
        [datePicker setMinimumDate:minDate];

hear the date of the current date on minDate and five years (ago) before maxDate it shows only five years .....

+3
source

You can also do this in the Interface Builder panel. I think that if you press "Command-1", it will switch to a tab in the palette that will allow you to set the maximum date for the collector.

+2
source

Source: https://habr.com/ru/post/1762192/


All Articles