How to set UIDatePicker to an initial date other than "Today"
Solution 1
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.
Solution 2
By doing this
[datePicker setDate:yourDate];
in viewDidLoad
method.
Solution 3
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.
Solution 4
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]];
![SpokaneDude](https://i.stack.imgur.com/g94sk.jpg?s=256&g=1)
SpokaneDude
I've been programming since 1966; worked on the first IBM S/360 in Los Angeles. Currrently retired from IBM, spend my time gardening, fishing, woodworking and coding iPhone apps (when the weather is too nasty to work outside).
Updated on July 09, 2022Comments
-
SpokaneDude almost 2 years
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
-
Jonathan Grynspan over 11 yearsActually,
[NSDate date]
gets the current timestamp in the form of a double representing the number of seconds since I-forget-the-exact-date in 2001 at midnight GMT. The YYYY-MM-DD stuff you're thinking about is just the default format used by the class'-description
method (used when printing an object to the console.) -
SpokaneDude over 11 yearsJonathan: I was thinking... if I set the default date to yesterday, which works... now, to train the users to change the date... Please change your comment to an answer... :D
-
rmaddy over 11 years@cory -
[NSDate date]
does not get the date in an specific format. What you claim is simply the result of logging a date object. Your whole last sentence is incorrect. And since aUIDatePicker
shows the current date by default, there is no need at all for the code you posted.