The following code displays an odd behavior under iOS 4.3 (maybe others version too). In this example, a UIDatePicker
whose date is set to 4 Aug 2011 2:31 PM
is displayed. The UILabel
below the UIDatePicker
displays the date for reference. The three UIButtons
below, labeled 1, 5, 10 set the minuteInterval
on the UIDatePicker
.
Tapping 1 - shows the selected date in the UIDatePicker
to be 4 Aug 2011 2:31 PM
, and the minute interval is 1, which is to be expected.
Tapping 5 - shows the selected date in the UIDatePicker
to be 4 Aug 2011 2:35 PM
, and the minute interval is 5, which is to be expected (one could argue the time should round down, but that is not a huge issue).
Tapping 10 - shows the selected date in the UIDatePicker
to be 4 Aug 2011 2:10 PM
, and the minute interval is 10. Okay the minute interval is correct, but the selected time is 2:10? One would have expected 2:40 (if rounded up) or 2:30 (if rounded down).
BugDatePickerVC.h
#import <UIKit/UIKit.h>
@interface BugDatePickerVC : UIViewController {
NSDateFormatter *dateFormatter;
NSDate *date;
UIDatePicker *datePicker;
UILabel *dateL;
UIButton *oneB;
UIButton *fiveB;
UIButton *tenB;
}
- (void) buttonEventTouchDown:(id)sender;
@end
BugDatePickerVC.m
import "BugDatePickerVC.h"
@implementation BugDatePickerVC
- (id) init
{
if ( !(self = [super init]) )
{
return self;
}
dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"d MMM yyyy h:mm a";
date = [[dateFormatter dateFromString:@"4 Aug 2011 2:31 PM"] retain];
// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
// Date picker
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 216.0f)];
datePicker.date = date;
datePicker.minuteInterval = 1;
[self.view addSubview:datePicker];
// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
// Label with the date.
dateL = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 230.0f, 300.0f, 32.0f)];
dateL.text = [dateFormatter stringFromDate:date];
[self.view addSubview:dateL];
// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
// Button that set the date picker's minute interval to 1.
oneB = [UIButton buttonWithType:UIButtonTypeRoundedRect];
oneB.frame = CGRectMake(10.0f, 270.0f, 100.0f, 32.0f);
oneB.tag = 1;
[oneB setTitle:@"1" forState:UIControlStateNormal];
[oneB addTarget:self
action:@selector(buttonEventTouchDown:)
forControlEvents:UIControlEventTouchDown];
[self.view addSubview:oneB];
// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
// Button that set the date picker's minute interval to 5.
fiveB = [UIButton buttonWithType:UIButtonTypeRoundedRect];
fiveB.frame = CGRectMake(10.0f, 310.0f, 100.0f, 32.0f);
fiveB.tag = 5;
[fiveB setTitle:@"5" forState:UIControlStateNormal];
[fiveB addTarget:self
action:@selector(buttonEventTouchDown:)
forControlEvents:UIControlEventTouchDown];
[self.view addSubview:fiveB];
// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
// Button that set the date picker's minute interval to 10.
tenB = [UIButton buttonWithType:UIButtonTypeRoundedRect];
tenB.frame = CGRectMake(10.0f, 350.0f, 100.0f, 32.0f);
tenB.tag = 10;
[tenB setTitle:@"10" forState:UIControlStateNormal];
[tenB addTarget:self
action:@selector(buttonEventTouchDown:)
forControlEvents:UIControlEventTouchDown];
[self.view addSubview:tenB];
return self;
}
- (void) dealloc
{
[dateFormatter release];
[date release];
[datePicker release];
[dateL release];
[oneB release];
[fiveB release];
[tenB release];
[super dealloc];
}
- (void) buttonEventTouchDown:(id)sender
{
datePicker.minuteInterval = [sender tag];
}
Here's yet another approach, with an Objective-C category!
I took the spirit of @zurbergram's rounding behavior (up/down to closest) and @mmorris's overall answer and came up with this category:
Then in your implementation, you can do something like this:
Bonus method: setMinimumDateRoundedByMinuteInterval: lets you set the picker's initial minimum to match the same behavior. One refactor would be to abstract the actual calculation part out into its own method, instead of the copy pasta, but I'm sure folks can optimize that for themselves.
zurbergram code in Swift :
I had the same problem with a UIDatePicker only with hours and minutes, every time I selected a time the picker add 20 minutes in the UI, not in the selected time. The solution was quite easy in my case, set the
picker.minuteInterval=5
before setting the value of the picker.Hope this will help to other people.
Swift 4 version
I used the above solution by mmoris and created a method that returns rounded date.. (for ARC)
Okay so I was able to change the behavior by explicitly setting the
UIDatePicker
date value to the date rounded to the minute interval using the following code:Pay attention to the part where the
UIDatePicker
's date is set twice. It was fun figuring that out.Anyone know how to turn the animation off for the call to
minuteInterval
? The phantom scrolling when clicking 5 then 10 is a little unsightly.