Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 744
Ram Avatar asked May 28 '14 11:05

Ram


People also ask

How do I turn off future date in calendar?

In the calendar widget their is an option to select max date, in which you can set the value as CurrDate(). So that future dates in the calendar would be disabled.

How do I restrict future dates in Datepicker?

If you want to future date disable from today use today. getFullYear() - 10 in order to disable 10 years before which means disable from 2008.

How do you turn off future dates in flutter?

In the Flutter date range picker, you can enable or disable the past dates by using the enablePastDates property of the calendar. When setting the enablePastDates property value as false, you can't able to select the past dates.


2 Answers

Here is the simple code to prevent future date selection:

dateofBirthDatePicker.maximumDate=[NSDate date]; 

In Swift :

dateofBirthDatePicker.maximumDate = Date() 
like image 170
Mawoon Avatar answered Oct 04 '22 17:10

Mawoon


In Swift 2:

self.datePicker.maximumDate = NSDate()

In Swift 3:

self.datePicker.maximumDate = Date()

like image 30
Phil Hudson Avatar answered Oct 04 '22 19:10

Phil Hudson