Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set UIDatePicker to an initial date other than "Today"

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

like image 661
SpokaneDude Avatar asked Jan 04 '13 20:01

SpokaneDude


4 Answers

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.

like image 61
cory ginsberg Avatar answered Nov 13 '22 09:11

cory ginsberg


By doing this

[datePicker setDate:yourDate];

in viewDidLoad method.

like image 23
Anoop Vaidya Avatar answered Nov 13 '22 11:11

Anoop Vaidya


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:

enter image description here

PS: Change the format if you want to show only parts of the date.

like image 1
Ramy Al Zuhouri Avatar answered Nov 13 '22 11:11

Ramy Al Zuhouri


NSDate *todayDate = [NSDate date];

NSDate *aGo = [todayDate dateByAddingTimeInterval:-6*24*60*60];
  1. It will set minimum date for date picker

    [_datePickerView setMinimumDate:aGo];

  2. When we open datepickerview the default date position is set here

    [_datePickerView setDate:[NSDate date]];

like image 1
Anisha V Avatar answered Nov 13 '22 11:11

Anisha V