DevC
DevC

Reputation: 7022

Set UIDatePicker minimumDate to x days from now, e.g 2 days from today

How can I set the minimumDate of the UIDatePicker to 'x' number of days away? For example, set the minimumDate to be two days from todays date.

I can set the minimumDate to today using;

 UIDatePicker * datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 320, 163)];
 datePicker.minimumDate=[NSDate date];

But I am unsure how to make it a day in the future.

Upvotes: 0

Views: 201

Answers (2)

nburk
nburk

Reputation: 22731

Use the following code:

 NSTimeInterval secondsPerHour = 3600;
 NSTimeInterval secondsPerDay = secondsPerHour * 24;
 UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 320, 163)];
 datePicker.minimumDate= [[NSDate date] dateByAddingTimeInterval:2*secondsPerDay];

The method that you are looking for is - (id)dateByAddingTimeInterval:(NSTimeInterval)seconds from NSDate, in the Apple docs you can find more info on it :)

Note that NSTimeInterval is always specified in seconds.

Upvotes: 2

bobnoble
bobnoble

Reputation: 5824

Create an NSDate with the date that is two days from the current date:

NSTimeInterval oneDay = 60 * 60 * 24;
NSDate *twoDaysFromNow = [NSDate dateWithTimeIntervalSinceNow:oneDay * 2];

Upvotes: 1

Related Questions