Nov 3, 200820

How To Make a jQuery Calendar

Whether you’re an airline, task manager, or just have a birthdate field on your registration form, a popup date picker makes selecting dates considerably easier for users, and overall enhances the functionality and feel of the site. jQuery UI provides a datepicker widget that can easily be incorporated into your site.

The Finished Datepicker Example

Getting Started

In order to use the datepicker, we need to download it from the jQuery UI download page and link it into our page as we do with the jQuery library. Applying the datepicker requires no HTML changes, and instead simply attaches itself to existing input fields. This is done with a single line of code:


	$('#trip input#leavedate').datepicker();
	

The standard date picker that is shown is admittedly fairly ugly and impractical in both size and look.

Standard date picker

Customization

The real power of jQuery’s datepicker is the level of customization. We’ll go through three quick steps toward creating a datepicker that’s right for our example situation. This should give you enough examples and variations for you to choose the right parameters for your situation, too.

Styling

First up, we’re going to make the calendar popup itself a little more appealing for the sake of making more practical decisions around the actual placement of the calendar. jQuery provides a handful of CSS stylesheets to chose from, but you can of course always write your own or modify them if you don’t like them. With the first theme, the calendar looks like this:

Date picker with default style

Date Format

Specifying the date format is just a matter of passing a parameter to the .datepicker() initialization function. Here are some common formats:


	$('#trip input#leavedate').datepicker({ dateFormat: $.datepicker.W3C }); // 2008-01-31
	$('#trip input#leavedate').datepicker({ dateFormat: 'mm/dd/y' }); // 01/31/08
	$('#trip input#leavedate').datepicker({ dateFormat: 'm/d/yy' }); // 1/31/2008
	$('#trip input#leavedate').datepicker({ dateFormat: 'M d, yy' }); // Jan 31, 2008
	$('#trip input#leavedate').datepicker({ dateFormat: 'MM d, yy' }); // January 31, 2008
	$('#trip input#leavedate').datepicker({ dateFormat: 'D, M d, yy' }); // Thurs, Jan 31, 2008
	

This leaves you with control over the order, separaters (slash, dash, or space), whether or not you want leading zeros on your days or months, whether you want numbers or words, and what you include or omit (year, day name, etc.).

These affect the format that the date shows up in the field after you select it:

Date picker with an alternate date format

Behavior

The last customization that we’re going to look at here is the popup behavior. By default, the event is attached to the focus of the input field that the datepicker widget is attached to. This can easily be changed so that the event is attached to either a button or an image next to the field instead.


	$('#trip input#leavedate').datepicker({ showOn: 'button', buttonImage: 'calendar.gif', buttonImageOnly: true });
	

This makes the calendar popup look like this:

Date picker with icon for behavior

Wrapping Everything Up

These customizations are what make the jQuery datepicker UI widget so powerful, as the widget is practical in a variety of situations. The following is the completed example using the customizations discussed above.

Tutorial kindly written by Nathan Wong

20 Comments

  • Max
    Nov 3, 2008

    This is great i needed to create something like this, this makes it so much easier, an looks like it degrades which is good.

  • Rachel
    Nov 5, 2008

    You explained this in a much easier-to-understand way than I have seen on other sites. Thank you!!!

    Rachel
    AllGraphicDesign.com

  • Ainsworth Studio
    Nov 6, 2008

    Thank you for this. Once again, you’ve written a very timely article for me. I love Design Reviver.

  • Nathan Wong
    Nov 6, 2008

    I’m glad you guys find it helpful. :)

  • DPW Graphic Design Services
    Nov 12, 2008

    great simple tutorial

    how do you find it compares to blueshoes (if you have also used that)?

  • IT Freak
    Nov 22, 2008

    This looks great, i’ve got it downloaded and can’t wait to use it. :)

  • steve
    Dec 2, 2008

    Hello Demo looks great and just what I need.

    However it would be perfect if when you select a from date and then went on to pick a to date, the to date pop up would not let you select and dates inclusive of the from date. Ie would only let you pick dates in the future from the original date.

    This update would make it perfect :-)

    Apart from that – brilliant tutorial

    Thanks

    Steve

  • Duane Nason
    Dec 5, 2008

    Nifty! I’m going to download it and give it a shot.

  • links for 2009-03-05
    Mar 5, 2009

    [...] How To Make a jQuery Calendar | Design Reviver (tags: Calendar jquery) [...]

  • Seven Beautiful JQuery scripts | DeveloperFox
    May 4, 2009

    [...] Site | Demo [...]

  • November Web Design Roundup #1 « Falcon Hive
    May 9, 2009

    [...] How To Make a jQuery Calendar [...]

  • Andrew S
    May 27, 2009

    How do you highlight arbitrary dates on the DatePicker i.e public holidays, days of interest etc!

  • Young
    Jun 27, 2009

    I love this article, thanks a lot!

  • Gerard
    Jul 9, 2009

    Check out http://keith-wood.name/datepick.html

    Its made by one of the original creators of the jquery ui datepicker, except this guy went and continued development, where as the datepicker has had a refactoring slated for as long as I’ve been working with it.

    So this one already accommodates date range and multiple date select, custom day styles, and blackout dates. If you run any sort of travel site, you’d know how important blackout date support on the calendar can be

  • Michel
    Aug 14, 2009

    Is that a way to make that datePicker a datetimePicker or do i have to create a timepicker apart?
    thank you

  • How To Make a jQuery Calendar | Design Reviver
    Aug 16, 2009

    [...] See original here: How To Make a jQuery Calendar | Design Reviver [...]

  • Calendario Popup con Jquery
    Sep 13, 2009

    [...] Ver Contenido‘ [...]

  • RoKoN BoSS
    Oct 10, 2009

    Lovely, thanks a lot!
    But i need totally a customized JCalendar.

  • vincent cabrera
    Dec 4, 2009

    hi

    im new to jquery and this article help make me appreciate it more. thanks ***** (5star)

    more power

  • Seven Beautiful JQuery Scripts « Nulls
    Jan 25, 2010

    [...] Site | Demo [...]

Leave a Comment

Ask a Question