iphonedatepickermaxdate

How to set Max date dynamically for date picker in iPhone?


I have date picker and i want to set maximum date as TODAY date which will change daily.
For ex today's date is 28/6/2011 so maximum date is 28/6/2011 but when i use my app tomorrow it will be change to 29/6/2011.
How to set this?
Thanks


Solution

  • Set the maximumDate property in viewWillAppear: method like this,

    datePicker.maximumDate = [NSDate date];
    

    To deal with date change while the application is being used, get the time left till tomorrow and set an NSTimer to trigger off at date change and then update the UIDatePicker instance.

    NSDate * tomorrow = [NSDate dateWithNaturalLanguageString:@"12 AM tomorrow"];
    NSTimeInterval timeInterval = [tomorrow timeIntervalSinceNow];
    
    /* Create an NSTimer to trigger a method to update the datePicker's maximumDate 
       after timeInterval */