IOS 5 - Setting the UIDatePicker Minimum Date to Today

I know how to set the maximum and minimum date in UIDatePicker for a specific date, and I am wondering if it is possible to make the minimum date always be “today's” date. In my application, it simply does not make sense to enter a date that was in the past, just to add what is today or in the future.

+6
source share
1 answer

set the minimum date at the moment, and the displayed date is now + 2 seconds:

myDatePicker.date = [[ NSDate alloc ] initWithTimeIntervalSinceNow: (NSTimeInterval) 2 ]; myDatePicker.minimumDate = [[ NSDate alloc ] initWithTimeIntervalSinceNow: (NSTimeInterval) 0 ]; 

Do this when you either start Nib or configure the data for the view controller when reused. You can also do this in viewWillAppear.

Extra points: you can add an IBAction method that subtly encourages the user not to choose a warning date:

 - (IBAction)datePickerChanged: (id)sender { if ( [ datePicker.date timeIntervalSinceNow ] < 0 ) datePicker.date = now; } 
+24
source

Source: https://habr.com/ru/post/918009/


All Articles