I need to set the initial date to something other than "Today" so the user is forced to make a selection. I have found that if the user does NOT make a selection, the date/time is set to "nil"; I can send an UIAlertView, but that is after the fact.
I have looked at SO and Google, but found nothing. How can I do this?
UPDATE: actually, I didn't state it, but the year is not shown, only month, day and am/pm. Without the year, they might not see a future date. T
One question, how do you know that the user will not pick that specific date that you set it to?
I would use
[datePicker setDate:[NSDate date]];
to display today's date on the date picker.
By doing this
[datePicker setDate:yourDate];
in viewDidLoad
method.
You can set a date in the future, and then set also a minimum date so that the user can only choose a date in the future (if that's what you're looking for).
Programmatically
If you are doing all this programmatically you should use these properties:
@property(nonatomic, retain) NSDate *maximumDate;
@property(nonatomic, retain) NSDate *minimumDate;
@property(nonatomic, retain) NSDate *date;
With Interface Builder
If you are doing it via xib (or storyboard) file, in the attributes inspector you can set all the attributes: the date, but also the minimum and maximum date, so if you want that the user can't set today as date, but only a date in the future, use the minimum date:
PS: Change the format if you want to show only parts of the date.
NSDate *todayDate = [NSDate date];
NSDate *aGo = [todayDate dateByAddingTimeInterval:-6*24*60*60];
It will set minimum date for date picker
[_datePickerView setMinimumDate:aGo];
When we open datepickerview the default date position is set here
[_datePickerView setDate:[NSDate date]];
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