The timepicker addon adds a timepicker to jQuery UI Datepicker, thus the datepicker and slider components (jQueryUI) are required for using any of these. In addition all datepicker options are still available through the timepicker addon.
If you are interested in contributing to Timepicker Addon please check it out on GitHub. If you do make additions please keep in mind I enjoy tabs over spaces,.. But contributions are welcome in any form.
Download/Contribute on GitHub (Find a bug? See if its fixed here)
There is a small bit of required CSS:
/* css for timepicker */ .ui-timepicker-div .ui-widget-header { margin-bottom: 8px; } .ui-timepicker-div dl { text-align: left; } .ui-timepicker-div dl dt { height: 25px; margin-bottom: -25px; } .ui-timepicker-div dl dd { margin: 0 10px 10px 65px; } .ui-timepicker-div td { font-size: 90%; } .ui-tpicker-grid-label { background: none; border: none; margin: 0; padding: 0; }
Has this Timepicker Addon been helpful to you?
Add a simple timepicker to jQuery UI's datepicker
$('#example1').datetimepicker();
Show time in AM/PM 12 hour format
$('#example2').datetimepicker({ ampm: true });
Show only the time picker without date picker
$('#example3').timepicker({});
Change the format (default is "hh:mm tt")
Also related to your timeFormat, but not required, is the separator option
$('#example4').datetimepicker({ timeFormat: 'h:m', separator: ' @ ' });
Show Seconds, Minutes, or Hours
$('#example5').datetimepicker({ showSecond: true, showMillisec: true, timeFormat: 'hh:mm:ss:l' });
Hours, Minutes, Seconds in steps
$('#example6').datetimepicker({ showSecond: true, timeFormat: 'hh:mm:ss', stepHour: 2, stepMinute: 10, stepSecond: 10 });
Default Hours, Minutes, Seconds
$('#example7').datetimepicker({ hour: 13, minute: 15 });
Set the time range. For instance if you only want to allow the user to choose times between 8AM and 5PM.
$('#example8').timepicker({ ampm: true, hourMin: 8, hourMax: 16 });
There is also localization support. Read up on localization for datepicker for adding support for more languages. There are a few new definitions you will need to add to your language file:
Or optionally you may add them as options as shown below with Russian.
$('#example9').timepicker({ timeOnlyTitle: 'Выберите время', timeText: 'Время', hourText: 'Часы', minuteText: 'Минуты', secondText: 'Секунды', currentText: 'Теперь', closeText: 'Закрыть' });
The first approach (as documented in the datepicker documentation and mentioned above) is to create your own regional objects per region, then use the setDefaults method to tie these together. Setting datepicker and timepicker regionals separtely will help ensure proper wording when only using datepicker or timepicker. Here is an example:
$.datepicker.regional['ru'] = { closeText: 'Закрыть', prevText: '<Пред', nextText: 'След>', currentText: 'Сегодня', monthNames: ['Январь','Февраль','Март','Апрель','Май','Июнь', 'Июль','Август','Сентябрь','Октябрь','Ноябрь','Декабрь'], monthNamesShort: ['Янв','Фев','Мар','Апр','Май','Июн', 'Июл','Авг','Сен','Окт','Ноя','Дек'], dayNames: ['воскресенье','понедельник','вторник','среда','четверг','пятница','суббота'], dayNamesShort: ['вск','пнд','втр','срд','чтв','птн','сбт'], dayNamesMin: ['Вс','Пн','Вт','Ср','Чт','Пт','Сб'], weekHeader: 'Не', dateFormat: 'dd.mm.yy', firstDay: 1, isRTL: false, showMonthAfterYear: false, yearSuffix: '' }; $.datepicker.setDefaults($.datepicker.regional['ru']); $.timepicker.regional['ru'] = { timeOnlyTitle: 'Выберите время', timeText: 'Время', hourText: 'Часы', minuteText: 'Минуты', secondText: 'Секунды', millisecText: 'миллисекунды', currentText: 'Теперь', closeText: 'Закрыть', ampm: false }; $.timepicker.setDefaults($.timepicker.regional['ru']);
To show numbered grids under the sliders you may use the following options:
When using ampm option hours will be displayed with an "a" or "p": 04a, 12p, etc...
$('#example10').timepicker({ hourGrid: 4, minuteGrid: 10 });
When showing more than one month:
$('#example11').datetimepicker({ numberOfMonths: 3 });
With minDate and maxDate datepicker options:
$('#example12').datetimepicker({ numberOfMonths: 2, minDate: 0, maxDate: 30 });
Get and Set Datetime:
var ex13 = $('#example13'); ex13.datetimepicker(); $('#example13_setdt').click(function(){ ex13.datetimepicker('setDate', (new Date()) ); }); $('#example13_getdt').click(function(){ alert(ex13.datetimepicker('getDate')); });
Use the datepicker minDate/maxDate options: (note: these must use Date objects)
$('#example14').datetimepicker({ minDate: new Date(2010, 11, 20, 8, 30), maxDate: new Date(2010, 11, 31, 17, 30) });
Connect to the onSelect event (Use your browser's js console for this example).
$('#example15').datetimepicker({ showSecond: true, showMillisec: true, timeFormat: 'hh:mm:ss:l', onSelect: function(dateText, inst){ if(window.console) console.log(dateText); } });
Create a datetime range with a start and end date.
$('#example16_start').datetimepicker({ onClose: function(dateText, inst) { var endDateTextBox = $('#example16_end'); if (endDateTextBox.val() != '') { var testStartDate = new Date(dateText); var testEndDate = new Date(endDateTextBox.val()); if (testStartDate > testEndDate) endDateTextBox.val(dateText); } else { endDateTextBox.val(dateText); } }, onSelect: function (selectedDateTime){ var start = $(this).datetimepicker('getDate'); $('#example16_end').datetimepicker('option', 'minDate', new Date(start.getTime())); } }); $('#example16_end').datetimepicker({ onClose: function(dateText, inst) { var startDateTextBox = $('#example16_start'); if (startDateTextBox.val() != '') { var testStartDate = new Date(startDateTextBox.val()); var testEndDate = new Date(dateText); if (testStartDate > testEndDate) startDateTextBox.val(dateText); } else { startDateTextBox.val(dateText); } }, onSelect: function (selectedDateTime){ var end = $(this).datetimepicker('getDate'); $('#example16_start').datetimepicker('option', 'maxDate', new Date(end.getTime()) ); } });
Use the sliderAccess addon to allow timepicker sliders to work on touch devices.
$('#example17').datetimepicker({ addSliderAccess: true, sliderAccessArgs: { touchonly: false } });
Use the utility function to format your own time. $.datepicker.formatTime(format, time, options)
$('#example18').text( $.datepicker.formatTime('hh:mm z', { hour: 14, minute: 36, timezone: '+2000' }, { ampm: false }) );
Version 1.0.1
Last updated on 07/01/2012
jQuery Timepicker Addon is currently available for use in all personal or commercial projects under both MIT and GPL licenses. This means that you can choose the license that best suits your project, and use it accordingly.