45

I am new to Objective C and IPhone development.

I am using a UIDatePicker in my IPhone Application and my requirement is to allow the user to choose the future date only from the DatePicker.

To do this i want to disable past date & time in DatePicker only instead of putting a compare validation in the code.

Is this possible or there is an option to disable the past date in the DatePicker control itself.

icodebuster
  • 8,890
  • 7
  • 62
  • 65
user658442
  • 459
  • 1
  • 4
  • 3

3 Answers3

149

Did you try this

[picker setMinimumDate: [NSDate date]];
visakh7
  • 26,380
  • 8
  • 55
  • 69
  • It's code snippets like this that make StackOverflow the best first stop on the web for all your coding needs! Instead of doing a check like in this post: "http://stackoverflow.com/questions/10671468/check-if-the-time-and-date-is-between-a-particular-date-and-time" I kept the search going and found this. Sure, the documentation is the primary source for properties like this, but it always helps SO users if posts like this don't exist yet. I'm upvoting the OP just so he has the chance to accept this answer. – whyoz Jun 11 '12 at 22:04
  • How to disable some time for picker when date picker in [datePicker setDatePickerMode:UIDatePickerModeTime]; – The iCoder Jan 08 '14 at 11:33
28

Use the following line,to disable the previous dates of an UIDatePicker.

[picker setMinimumDate: [NSDate date]];

The current date is displayed as shown below

enter image description here

Madhu
  • 2,565
  • 2
  • 25
  • 32
27

For Swift :

let todaysDate = Date()

datePicker.minimumDate = todaysDate

It will not let the user to pick a date older than Today

OR

Or, if you want to reduce the code, use following way:

datePicker.minimumDate = Date()