Wednesday, October 29, 2014

Restrict user from selecting future date in UIDatePickerView

Below function disables the future date in UIDatePickerView 

-(void)setMaximumDateInDatePicker {       

    [dateTimePicker setMinuteInterval:1];   
    
    NSDate* now = [NSDate date] ;
    NSCalendar* calendar = [NSCalendar currentCalendar] ;

    NSDateComponents* nowWithoutSecondsComponents = [calendar components:(NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit) fromDate:now] ;

    int currentHour=nowWithoutSecondsComponents.hour;
    int currentMinute=nowWithoutSecondsComponents.minute;
    int currentSecond=nowWithoutSecondsComponents.second;
  
    NSDate *maxDate = [[NSDate alloc] initWithTimeIntervalSinceNow:((24-currentHour)*60*60)-((currentMinute*60)+currentSecond+10)];

    dateTimePicker.maximumDate = maxDate ;
    NSLog(@"maxDate : %@ ",maxDate );

}

No comments:

Post a Comment