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; }
You also need to include jQuery UI with datepicker and slider wigits. Order: jQuery, jQueryUI, Timepicker.
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: 'Миллисекунды', timezoneText: 'Часовой пояс', currentText: 'Сейчас', closeText: 'Закрыть', timeFormat: 'hh:mm tt', amNames: ['AM', 'A'], pmNames: ['PM', 'P'], 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) });
The following demonstrates how to use timezones and timezone options.
The third example here demonstrates using the abbreviation for timezones. If this is used defaultTimezone must match the value (abbreviation). Do note that useLocalTimezone may not function properly when using abbreviations as it will only work with '+0000' formats.
Daylight savings time may be interpreted as a timezone. Create your own timezoneList and specify entries for Daylight Savings or use useLocalTimezone to help get the correct offset.
$('#example15a').datetimepicker({ timeFormat: 'hh:mm z', showTimezone: true }); $('#example15b').datetimepicker({ timeFormat: 'hh:mm z', showTimezone: true, timezoneList: [ { value: '-0500', label: 'Eastern'}, { value: '-0600', label: 'Central' }, { value: '-0700', label: 'Mountain' }, { value: '-0800', label: 'Pacific' } ] }); $('#example15c').datetimepicker({ timeFormat: 'hh:mm z', showTimezone: true, timezone: 'MT', timezoneList: [ { value: 'ET', label: 'Eastern'}, { value: 'CT', label: 'Central' }, { value: 'MT', label: 'Mountain' }, { value: 'PT', label: 'Pacific' } ] });
Connect to the onSelect event (Use your browser's js console for this example).
$('#example16').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.
var startDateTextBox = $('#example17_start'); var endDateTextBox = $('#example17_end'); startDateTextBox.datetimepicker({ onClose: function(dateText, inst) { if (endDateTextBox.val() != '') { var testStartDate = startDateTextBox.datetimepicker('getDate'); var testEndDate = endDateTextBox.datetimepicker('getDate'); if (testStartDate > testEndDate) endDateTextBox.datetimepicker('setDate', testStartDate); } else { endDateTextBox.val(dateText); } }, onSelect: function (selectedDateTime){ endDateTextBox.datetimepicker('option', 'minDate', startDateTextBox.datetimepicker('getDate') ); } }); endDateTextBox.datetimepicker({ onClose: function(dateText, inst) { if (startDateTextBox.val() != '') { var testStartDate = startDateTextBox.datetimepicker('getDate'); var testEndDate = endDateTextBox.datetimepicker('getDate'); if (testStartDate > testEndDate) startDateTextBox.datetimepicker('setDate', testEndDate); } else { startDateTextBox.val(dateText); } }, onSelect: function (selectedDateTime){ startDateTextBox.datetimepicker('option', 'maxDate', endDateTextBox.datetimepicker('getDate') ); } });
Use the sliderAccess addon to allow timepicker sliders to work on touch devices.
$('#example18').datetimepicker({ addSliderAccess: true, sliderAccessArgs: { touchonly: false } });
Use the utility function to format your own time. $.datepicker.formatTime(format, time, options)
$('#example19').text( $.datepicker.formatTime('hh:mm z', { hour: 14, minute: 36, timezone: '+2000' }, { ampm: false }) );
Version 1.0.2
Last updated on 09/8/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.