Here’s my code:
picker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,40,0,0)];
picker.datePickerMode = UIDatePickerModeDateAndTime;
picker.minuteInterval = 5;
picker.minimumDate = [NSDate date];
Ok, It’s working fine until here. (Image: http://img29.imageshack.us/img29/8277/snap1r.png)
Days that are past in the DatePicker were all grayed out. It can’t be selected.
And minute Intervals are 5.
But now when I click any row that were already Grayed out. The date of DatePicker returns time of this moment.
For Example:I cliked “9” on the DatePicker (It’s already past time)
And the system time now is
22:27:57
and the Date of DatePicker returns: (Image: http://img42.imageshack.us/img42/2760/nslog.png)
2012-04-08 22:27
Because my minute interval is 5 minute, so I don’t hope the picker returns the value that can’t be divided by 5, this will cause my program crash.
Is this a Bug? or it’s just my problem?
thanks!
——To inspector g (Sorry my English isn’t very good)
Because the minuteInterval of Datepicker is 5. So the return value of DatePicker’s date only returns minute that can divded by 5 (etc. 0, 5 , 10 , 15 …..)
and also I have the property minimumDate set to [NSDate date], so that users can’t select the date in past.
but now of user click a row that was in past (grayed out), the DatePicker’s date return the time at that moment.
so the minute of date could be any value (0~60) but not I wished ( 0 , 5 , 10 , 15….)
I’ve tried my best to explain >”< please forgive.
To Inspector g.
Thanks for your code, I suddenly realized there’s a nice way to solve my problem.
But I dunno why, there are some problems if I use your code. (I guess it’s about timeZone)
But I follow your logic and re-write a code, I’ll share with you:
unsigned unitFlags_ = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit;
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps_ = [gregorian components:unitFlags_ fromDate:[jRemindPicker date]];
NSInteger remainder = [comps_ minute] % 5;
NSLog(@"%i-%i-%i %i:%i", comps_.year, comps_.month, comps_.day, comps_.hour, comps_.minute);
if ( remainder ) {
/* My Own code /*
} else {
/* My Own Code /*
}
[gregorian release];
Your description of the problem with selecting a date/time is a little unclear, so perhaps you can clarify? Provide a short screencast?
In any event, it sounds like you cannot select a date before today, so your error is in this line:
You are setting the minimum selectable date to the current date and time (as that’s what
[NSDate date]returns.Remove that line and you should be able to select whatever date/time you wish.
EDIT
If the problem is that you can’t select a date in the future, try setting:
Using your existing minimum and this new maximum, the range of selectable dates will be set to somewhere between today and a very long time after today.
SECOND EDIT
Thanks for clarifying! I see the problem now. When you receive the callback from the user changing the date, you have to round up or down appropriately. You can then use the rounded time at that point, or manually set the picker date/time to the rounded value via
setDate: animated:For example: