About

Add datepicker picker to field or to any other element.

  • can be used as a component
  • formats: dd, d, mm, m, yyyy, yy
  • separators: -, /, .

Download

Example

Attached to a field with the format specified via options.

Attached to a field with the format specified via data tag.

As component.

Attached to other elment then field and using events to work with the date values.

Oh snap!
Start dateChange End dateChange
2012-02-20 2012-02-25

Attached to div (inline)


Using bootstrap-datepicker.js

Call the datepicker via javascript:

$('.datepicker').datepicker()

Options

Name type default description
format string 'mm/dd/yyyy' the date format, combination of d, dd, m, mm, yy, yyy.
weekStart integer 0 day of the week start. 0 for Sunday - 6 for Saturday
startDate date null The earliest date that may be selected; all earlier dates will be disabled
endDate date null The latest date that may be selected; all later dates will be disabled
autoclose boolean false Whether or not to close the datepicker immediately when a date is selected
startView string 0, 'month' The view that the datepicker should show when it is opened. Accepts values of 0 or 'month' for month view (the default), 1 or 'year' for the 12-month overview, and 2 or 'decade' for the 10-year overview. Useful for date-of-birth datepickers
keyboardNavigation boolean true Whether or not to allow date navigation by arrow keys
language string en The two-letter code of the language to use for month and day names. These will also be used as the input's value (and subsequently sent to the server in the case of form submissions). Currently ships with English ('en'), German ('de'), Brazilian ('br'), and Spanish ('es') translations, but others can be added (see I18N below). If an unknown language code is given, English will be used

Markup

Format a component.

<div class="input-append date" id="dp3" data-date="12-02-2012" data-date-format="dd-mm-yyyy">
  <input class="span2" size="16" type="text" value="12-02-2012">
  <span class="add-on"><i class="icon-th"></i></span>
</div>

Format inline.

<div id="dp6" data-date="12-02-2012" data-date-format="dd-mm-yyyy"></div>

Methods

.datepicker(options)

Initializes an datepicker.

show

Arguments: None.
Show the datepicker.

$('#datepicker').datepicker('show');

hide

Arguments: None.
Hide the datepicker..

$('#datepicker').datepicker('hide');

update

Arguments:

  • date (string, optional)

Update the datepicker with the current input value or given date as argument. In second case input will be updated as well.

$('#datepicker').datepicker('update');
$('#datepicker').datepicker('update', '15-05-1984');

setStartDate

Arguments:

  • startDate (string, optional)

Sets a new lower date limit on the datepicker.

$('#datepicker').datepicker('setStartDate', '2012-01-01');

Omit startDate (or provide an otherwise falsey value) to unset the limit.

$('#datepicker').datepicker('setStartDate');

setEndDate

Arguments:

  • endDate (string, optional)

Sets a new upper date limit on the datepicker.

$('#datepicker').datepicker('setEndDate', '2012-01-01');

Omit endDate (or provide an otherwise falsey value) to unset the limit.

$('#datepicker').datepicker('setEndDate');

Events

Datepicker class exposes a few events for manipulating the dates.

show

This event fires immediately when the date picker is displayed.

hide

This event is fired immediately when the date picker is hidden.

changeDate

This event is fired when the date is changed.

$('#dp5').datepicker()
  .on('changeDate', function(ev){
    if (ev.date.valueOf() < startDate.valueOf()){
      ....
    }
  });

changeYear

Fired when the view year is changed from decade view.

changeMonth

Fired when the view month is changed from year view.

Keyboard support

The datepicker includes some keyboard navigation:

up, down, left, right arrow keys

By themselves, left/right will move backward/forward one day, up/down will move back/forward one week.

With the shift key, up/left will move backward one month, down/right will move forward one month.

With the ctrl key, up/left will move backward one year, down/right will move forward oone year.

Shift+ctrl behaves the same as ctrl -- that is, it does not change both month and year simultaneously, only the year.

escape

The escape key can be used to hide and re-show the datepicker; this is necessary if the user wants to manually edit the value.

enter

When the picker is visible, enter will simply hide it. When the picker is not visible, enter will have normal effects -- submitting the current form, etc.

I18N

The plugin supports i18n for the month and weekday names and the weekStart option. The default is English ('en'); other available translations are avilable in the js/locales/ directory, simply include your desired locale after the plugin. To add more languages, simply add a key to $.fn.datepicker.dates , before calling .datepicker() . Example:

$.fn.datepicker.dates['en'] = {
    days: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
    daysShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
    daysMin: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"],
    months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
    monthsShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
};

If your browser (or those of your users) is displaying characters wrong, chances are the browser is loading the javascript file with a non-unicode encoding. Simply add charset="UTF-8" to your script tag:

<script type="text/javascript" src="bootstrap-datepicker.de.js" charset="UTF-8"></script>

This page is based on original datepicker by Stefan Petre www.eyecon.ro/bootstrap-datepicker and @eternicode's improvements.

Vitaliy Potapov, 2012