Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6227063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:08:57+00:00 2026-05-24T09:08:57+00:00

The following code displays an odd behavior under iOS 4.3 (maybe others version too).

  • 0

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];
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:08:58+00:00Added an answer on May 24, 2026 at 9:08 am

    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:

    - (void) handleUIControlEventTouchDown:(id)sender
    {
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        // Set the date picker's minute interval.
        NSInteger minuteInterval  = [sender tag];
    
        // Setting the date picker's minute interval can change what is selected on
        // the date picker's UI to a wrong date, it does not effect the date
        // picker's date value.
        //
        // For example the date picker's date value is 2:31 and then minute interval
        // is set to 10.  The date value is still 2:31, but 2:10 is selected on the
        // UI, not 2:40 (rounded up) or 2:30 (rounded down).
        //
        // The code that follow's setting the date picker's minute interval
        // addresses fixing the date value (and the selected date on the UI display)
        // .  In the example above both would be 2:30.
        datePicker.minuteInterval = minuteInterval;
    
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        // Calculate the proper date value (and the date to be selected on the UI
        // display) by rounding down to the nearest minute interval.
        NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSMinuteCalendarUnit fromDate:date];
        NSInteger minutes = [dateComponents minute];
        NSInteger minutesRounded = ( (NSInteger)(minutes / minuteInterval) ) * minuteInterval;
        NSDate *roundedDate = [[NSDate alloc] initWithTimeInterval:60.0 * (minutesRounded - minutes) sinceDate:date];
    
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        // Set the date picker's value (and the selected date on the UI display) to
        // the rounded date.
        if ([roundedDate isEqualToDate:datePicker.date])
        {
            // We need to set the date picker's value to something different than
            // the rounded date, because the second call to set the date picker's
            // date with the same value is ignored. Which could be bad since the
            // call above to set the date picker's minute interval can leave the
            // date picker with the wrong selected date (the whole reason why we are
            // doing this).
            NSDate *diffrentDate = [[NSDate alloc] initWithTimeInterval:60 sinceDate:roundedDate];
            datePicker.date = diffrentDate;
            [diffrentDate release];
        }
        datePicker.date = roundedDate;
        [roundedDate release];
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following R code and am seeing odd behavior - when I
I have the following XAML code, which displays a picture (image inside borders) and
I made the following code example to learn how to use a generics method
I have the following code that shows a panel. It displays a button on
When I use the following code in Android 1.6 it displays both phone numbers
I have the following code that fails to display object images. But displays normal
I have the following code, which i've simplified, where $currEl gets logged and displays
I have Designed the Javascript function My.js it contains following code My.js function display(){
Following code, when compiled and run with g++, prints '1' twice, whereas I expect
The following code works great in IE, but not in FF or Safari. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.