iPhone date and time picker view

I was looking around for a view that somebody had previously put together to show a modal view to allow the selection of a date and time, and found nothing.  This seemed like something reasonable to expect to find.  Apparently, none of the umpteen thousand fart application developers have ever had a need to create a date and time picker view, or if they did, they did not feel the need to share their creation with the world at large.

So here it is.  This is what it looks like when you use it in your application:

Here is the code that you would use to instantiate the view in your parent view controller, where selectedDate is the default date that you want the view to display when it opens up:

DateTimePicker *controller = [[DateTimePicker alloc] init];
controller.delegate = self;
controller.defaultDate = selectedDate;
[self presentModalViewController:controller animated:YES];
[controller release];

To receive messages back from this modal view, you just need your parent view to follow the DateTimePickerDelegate delegate, which means you would need to have the dateTimePickerOK and dateTimePickerOK methods in your parent view.  These methods are called when the user taps the Save or Cancel buttons respectively on the modal view.  Here is what those methods could look like:

- (void)dateTimePickerOK:(DateTimePicker *)controller didPickDate:(NSDate *)date
    self.selectedDate = date;
    [self redrawDate];
    [controller dismissModalViewControllerAnimated:YES];
- (void)dateTimePickerCancel:(DateTimePicker *)controller
    [controller dismissModalViewControllerAnimated:YES];

I have put together a sample application that demonstrates the use of the date and time picker view, which also of course includes the DateTimePicker header, implementation, and NIB files.  Please feel free to use these in our own code, and if you make any cool changes to it, please let me know so that I might include them in my example or ridicule you in the posting comments.

The sample code zip file is here:



  1. Paolo Denti says:

    thanks for the very interesting example.
    However in my opinion there is a mistake in it: the designated initializer for the UIViewController is initWithNibName:bundle:

    Therefore your code snippet
    DateTimePicker *controller = [[DateTimePicker alloc] init];
    should be
    DateTimePicker *controller = [[DateTimePicker alloc] initWithNibName:@”DateTimePicker” bundle:nil];

    Paolo Denti

  2. BP says:

    I usually use the initWithNibName initializer on my view controllers, but just the plain old init seems to work here. Did the example code not work for you? (I am on the 4.1 iOS SDK.)

    I am just guessing here, but it must have something to do with the File’s Owner of the NIB set to the class of the modal controller. If anyone has an explanation, please let me know as I would be interested to hear how it works.

  3. Paolo Denti says:

    Yes, it works even with init, but in my opinion you cannot rely on something that “just works” or “seems working”. The behavior of init is simply unspecified & undocumented, it could change in a future ios release.

  4. Alex says:

    Thanks for the sample, This is great, i believe that initWithNibName is not always required, but in some instance you might want to use it, specially if your setting the of the view and ur using either navigation or tab..

  5. Gauraw Mishra says:

    I m new a iPhone development.I did as demonstrated above and it’s very helpfull.
    Thanks for the tutorial and please keep updating new topics on iphone.

  6. nikunj says:

    this is good…..but my requirement is different…and this is sort discription…..you have to update ….because current market is working on ios7…this is old….

  7. BP says:

    Yeah, this is a bit old, and I’m sure that there are probably a lot of better open source controls you could find for your purpose. Check cocoacontrols.com as they have a lot of great stuff on that web site.

  8. raj says:

    Awesome Buddy.

Leave a Reply