How to set Time Range in UIDatePickerModeTime

2019-07-18 04:08发布

问题:

Hello friends I am trying to set the Time Range Of the DatePicker in UIDatePickerModeTime,as we have MaxDate and MinDate,whether its possible to set Mintime and Maxtime selected by user,,

Suppose I want the user to select the time between 16:00 to 19:00 only for each day then how can I do it,,

Thanks&Regards Ranjit

Hello friends I tried the below code to set the start time range and its working fine...

NSDate *todaysDate =[NSDate date];

NSDateComponents *comps = [gregorian components:NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit |NSSecondCalendarUnit fromDate:todaysDate];


[comps setHour:16];
[comps setMinute:00];
[comps setSecond:00];

 NSDate *startDate = [gregorian dateFromComponents:comps];

 NSDateComponents *startcomps = [[NSDateComponents alloc] init];
[startcomps setDay:startDay-weekday];


 self.minDate = [gregorian dateByAddingComponents:startcomps toDate:startDate   options:0];

but when I apply the same code to set the end time range its not working....can any body please help me out in this matter

Regards Ranjit

回答1:

Ok, I solved your problem (using IB, but I think you could do that programmatically too).

Using IB:

  1. Select your Date picker.
  2. Open Utilities -> Attribute inspector
  3. In block Constraints :
  4. Select Minimum Date and set its value 01/01/1970 16:00:00
  5. Select Maximum Date and set its value 01/01/1970 19:00:00
  6. Set value Date to 01/01/1970 16:00:00


标签: ios ios-4.2 ios4