I know how to set the maximum and minimum date in a UIDatePicker to a certain date, put I was wondering if it would be possible to make it so the minimum date is always the "today" date. It just doesn't make sense in my app for users to enter a date that's in the past, just for them to add one that is today or in the future.
set the minimum date to now and the displayed date to now + 2 secs:
myDatePicker.date =
[[ NSDate alloc ] initWithTimeIntervalSinceNow: (NSTimeInterval) 2 ];
myDatePicker.minimumDate =
[[ NSDate alloc ] initWithTimeIntervalSinceNow: (NSTimeInterval) 0 ];
Do this when you either init the Nib or set the data for the view controller on re-use. You may also do this in viewWillAppear.
Extra points: You may add a IBAction method that subtly encourages the user not to pick an ill advised date:
- (IBAction)datePickerChanged: (id)sender
{
if ( [ datePicker.date timeIntervalSinceNow ] < 0 )
datePicker.date = now;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With