0

I am trying to implement UIDatePicker for Birthday, and I want to limit Maximum date to today.

Here is my code:

UIDatePicker * datePicker =  [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 30, [UIScreen mainScreen].bounds.size.width - 50, 200)];
[datePicker setDatePickerMode:UIDatePickerModeDate];
[datePicker setMaximumDate:maxDate];
[self.view addSubview:datePicker];

setMaximumDate is totally not working in UIDatePickerModeDate mode for UIDatePicker in ObjectiveC

Please help, Thanks in advance.

Prasad Khode
  • 6,602
  • 11
  • 44
  • 59
Shunli
  • 23
  • 1
  • 4

1 Answers1

0

Set a maximumDate of one month from now

NSDate* now = [NSDate date] ;
// Get current NSDate without seconds & milliseconds, so that I can better compare the chosen date to the minimum & maximum dates.
NSCalendar* calendar = [NSCalendar currentCalendar] ;
NSDateComponents* nowWithoutSecondsComponents = [calendar components:(NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit) fromDate:now] ;
NSDate* nowWithoutSeconds = [calendar dateFromComponents:nowWithoutSecondsComponents] ;
//  UIDatePicker* picker ;
picker.minimumDate = nowWithoutSeconds ;

NSDateComponents* addOneMonthComponents = [NSDateComponents new] ;
addOneMonthComponents.month = 1 ;
NSDate* oneMonthFromNowWithoutSeconds = [calendar dateByAddingComponents:addOneMonthComponents toDate:nowWithoutSeconds options:0] ;
picker.maximumDate = oneMonthFromNowWithoutSeconds ;
vipinsaini0
  • 541
  • 1
  • 7
  • 26