User1075
User1075

Reputation: 885

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.

Upvotes: 0

Views: 1442

Answers (4)

Mehul
Mehul

Reputation: 3178

  //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];    

Upvotes: 1

Hardik Vyas
Hardik Vyas

Reputation: 2253

I Think This Will Help You

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:30];
NSDate *maxDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[comps setYear:-30];
NSDate *minDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];

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

Upvotes: 0

Pradeep Singh
Pradeep Singh

Reputation: 1280

There is property of UIDatePicker

@property(nonatomic, retain) NSDate *maximumDate

For more property visit the link

Upvotes: 3

Mee
Mee

Reputation: 173

In attribute inspector u have an option to set the maximum.

Upvotes: 1

Related Questions