Ram
Ram

Reputation: 804

How to disable future dates,months and year in UIDatePicker?

How to hide the future dates in UIDatePicker for user choosing only past and current year of birthdays.

I search lot of source but I can't get the desired result.

Here is my code,

 dateofBirthDatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];

    dateofBirthDatePicker.datePickerMode = UIDatePickerModeDate;
    [dateofBirthDatePicker setBackgroundColor:DATE_PICKER_GRAY_COLOR];

//    UILabel *label = [UILabel appearanceWhenContainedIn:[UITableView class],[UIDatePicker class], nil];
//    label.font = HELVETICA_NEUE(24);
//    label.textColor = GREEN_COLOR;
    [dateofBirthDatePicker addTarget:self
                              action:@selector(LabelChange:)
                    forControlEvents:UIControlEventValueChanged];
    dateofbirthTextField.inputView = dateofBirthDatePicker;
    [self datePickerToolBar];
}

- (void)LabelChange:(id)sender
{
    NSDateFormatter *dateFormat= [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];
    dateofbirthTextField.text = [NSString stringWithFormat:@"%@",
                                [dateFormat stringFromDate:dateofBirthDatePicker.date]];
}

If any body know the solution kindly give the suggestion. I really appreciate to you.

Upvotes: 23

Views: 28080

Answers (7)

Amal T S
Amal T S

Reputation: 3405

SwiftUI,

For disabling future dates

DatePicker("Select a Date",
                   selection: $selectedDate,
                   in: Date.distantPast...maxDate,
                   displayedComponents: [.date])
        
        

and for disabling past dates

DatePicker("Select a Date",
                   selection: $selectedDate,
                   in: minDate...Date.distantFuture,
                   displayedComponents: [.date])

Upvotes: 0

Jakir Hossain
Jakir Hossain

Reputation: 1072

In SwiftUI inline DatePicker:

DatePicker("Select Date", selection: $date, in: ...Date())

Upvotes: 0

Mawoon
Mawoon

Reputation: 1045

Here is the simple code to prevent future date selection:

dateofBirthDatePicker.maximumDate=[NSDate date];

In Swift :

dateofBirthDatePicker.maximumDate = Date()

Upvotes: 65

Keshav Gera
Keshav Gera

Reputation: 11244

In Swift 4.0.3 Xcode 9

Hide Future Date

self.picker.maximumDate = Date()

Upvotes: 9

Phil Hudson
Phil Hudson

Reputation: 3901

In Swift 2:

self.datePicker.maximumDate = NSDate()

In Swift 3:

self.datePicker.maximumDate = Date()

Upvotes: 14

Pancho
Pancho

Reputation: 4143

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:1];
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

Greg
Greg

Reputation: 25459

You can use setMaximumDate: and setMinimumDate: on UIDatePicker object:

[dateofBirthDatePicker setMaximumDate:[NSDate date]]; // The max date will be today

// Optional you can set up min date as well
[dateofBirthDatePicker setMinimumDate:yourMinDate];

Upvotes: 3

Related Questions