Search code examples
iosobjective-cuipickerviewuidatepicker

How to fix the maximum year in UIDatePicker but not date and month?


I can set the maximum date through [NSDate date] in UIPicker. But is there any way to set the maximum year only. And one more thing i want the dates and month to roll but the year should be maximum to 2015 only.


Solution

  •   //Get Current Date
        NSDate *currentTime = [NSDate date];
        [DatePicker setMinimumDate:currentTime];
    

    try to Set Maximum Date, it will completely set max to Year

        NSString *dateString = @"01-02-2010";
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        // this is imporant - we set our input date format to match our input string
        // if format doesn't match you'll get nil from your string, so be careful
        [dateFormatter setDateFormat:@"dd-MM-yyyy"];
        NSDate *dateFromString = [[NSDate alloc] init];
    
        dateFromString = [dateFormatter dateFromString:dateFromString];
        [datePicker setMaximumDate: dateFromString];