开发者

How to set time range in a day using UIDatePickerView

开发者 https://www.devze.com 2023-04-08 10:23 出处:网络
I am using UIDatePickerView, just like we can set the date range using \"maximum开发者_开发知识库Date\" and \"minimumDate\", is there any way to set time range in a day like from 9.00 am to 6.00 pm ?N

I am using UIDatePickerView, just like we can set the date range using "maximum开发者_开发知识库Date" and "minimumDate", is there any way to set time range in a day like from 9.00 am to 6.00 pm ?


Not really. minimumDate and maximumDate can include a time of day (NSDate is more like a timestamp than a calendar date), but this only has an effect if they are both on the same day because otherwise all (clock) times are possible within the date range.


Actually you should set the time using these two as well. NSDate objects have both date and time components. You should do something like this:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];

//set the date using setDate: setMonth: setYear:
[dateComponents setHour:9];    //setHour:18 for maximumDate

NSDate *targetDate = [gregorian dateFromComponents::dateComponents];
[dateComponents release];
[gregorian release];
datepick.minimumDate = targetDate;

If that doesnt help, have a look at these Qs:

UI Datepicker range. iPhone

How to set time on NSDate?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号