1

I have a UIDatePicker that I have programmatically added into my code, I'm now looking to see if I can somehow set a range on the time. So that the user can only select a date, and a time between the hours of 09:00 and 17:30. Any other options would ideally not show up. Is there a way I can add this into my UIDatePicker code?

Here is my code:

CGRect pickerFrame = CGRectMake(0,250,0,0);

UIDatePicker *myPicker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
[myPicker addTarget:self action:@selector(pickerChanged:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:myPicker];
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Luke
  • 55
  • 8
  • You can not select the options other than the range you set.Hope this will work: [Check the link](http://stackoverflow.com/questions/6857697/ui-datepicker-range-iphone) – Ritu Jul 16 '14 at 09:14
  • 2
    Just set the `minimumDate` and `maximumDate` properties. – trojanfoe Jul 16 '14 at 09:18
  • See this answer http://stackoverflow.com/questions/16322437/how-to-set-minimum-time-to-appear-as-default-in-uidatepicker-everytime – Alex Andrews Jul 16 '14 at 09:22
  • @Luke please remember to accept an answer if it is correct. – Fogmeister Jul 17 '14 at 12:50

2 Answers2

2

UIDatePicker has properties for minimumDate and maximumDate.

You should get used to reading the documentation.

Normally just sticking the class name into google the first result will be the Apple documentation.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
0

Try like this :-

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:10];
NSDate *maxDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[comps setYear:-10];
NSDate *minDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];    
[yourdatePicker setMaximumDate:maxDate];
[yourdatePicker setMinimumDate:minDate];
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56