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.
Back to Blog or Follow on Twitter
Has this Timepicker Addon been helpful to you?
Subscribe to my blog via email and follow @PracticalWeb on Twitter. I post for nearly every new version, so you know about updates.
Download/Contribute on GitHub (Need the entire repo? Find a bug? See if its fixed here)
There is a small bit of required CSS (Download):
/* 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 and jQuery UI with datepicker and slider wigits. You should include them in your page in the following order:
Version 1.0.3
Last updated on 09/15/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.
The timepicker does inherit all options from datepicker. However, there are many options that are shared by them both, and many timepicker only options:
The default format is "hh:mm tt", however ampm is turned off by default, so you will see "hh:mm"
Formats are used in the following ways:
Timepicker comes with many translations and localizations, thanks to all the contributors. They can be found in the localization folder in the git repo.
The quick and cheap way to use localizations is to pass in options to a timepicker instance:
$('#example123').timepicker({ timeOnlyTitle: 'Выберите время', timeText: 'Время', hourText: 'Часы', minuteText: 'Минуты', secondText: 'Секунды', currentText: 'Теперь', closeText: 'Закрыть' });
However, if you plan to use timepicker extensively you will need to include (build your own) localization. It is simply assigning those same variables to an object. As you see in the example below we maintain a separate object for timepicker. This way we aren't bound to any changes within datepicker.
$.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']);
Now all you have to do is call timepicker and the Russian localization is used. Generally you only need to include the localization file, it will setDefaults() for you.
You can also visit localization for datepicker for more information about datepicker localizations.
Add a simple datetimepicker to jQuery UI's datepicker
$('#basic_example_1').datetimepicker();
Add only a timepicker:
$('#basic_example_2').timepicker();
Format the time:
$('#basic_example_3').datetimepicker({ timeFormat: "h:m", ampm: true });
Simplest timezone usage:
$('#timezone_example_1').datetimepicker({ timeFormat: 'hh:mm z', showTimezone: true });
Define your own timezone options:
$('#timezone_example_2').datetimepicker({ timeFormat: 'hh:mm z', showTimezone: true, timezoneList: [ { value: '-0500', label: 'Eastern'}, { value: '-0600', label: 'Central' }, { value: '-0700', label: 'Mountain' }, { value: '-0800', label: 'Pacific' } ] });
Use timezone string abbreviations for values:
$('#timezone_example_3').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' } ] });
Add a grid to each slider:
$('#slider_example_1').timepicker({ hourGrid: 4, minuteGrid: 10 });
Set the interval step of sliders:
$('#slider_example_2').datetimepicker({ showSecond: true, timeFormat: 'hh:mm:ss', stepHour: 2, stepMinute: 10, stepSecond: 10 });
Add sliderAccess plugin for touch devices:
$('#slider_example_3').datetimepicker({ addSliderAccess: true, sliderAccessArgs: { touchonly: false } });
Alt field in the simplest form:
$('#alt_example_1').datetimepicker({ altField: "#alt_example_1_alt" });
With datetime in both:
$('#alt_example_2').datetimepicker({ altField: "#alt_example_2_alt", altFieldTimeOnly: false });
Format the altField differently:
$('#alt_example_3').datetimepicker({ altField: "#alt_example_3_alt", altFieldTimeOnly: false, altFormat: "yy-mm-dd", altTimeFormat: "h:m", altSeparator: " @ " });
Set the min/max hour of every date:
$('#rest_example_1').timepicker({ ampm: true, hourMin: 8, hourMax: 16 });
Set the min/max date numerically:
$('#rest_example_2').datetimepicker({ numberOfMonths: 2, minDate: 0, maxDate: 30 });
Set the min/max date and time with a Date object:
$('#rest_example_3').datetimepicker({ minDate: new Date(2010, 11, 20, 8, 30), maxDate: new Date(2010, 11, 31, 17, 30) });
Restrict a start and end date (also shows using onSelect and onClose events):
var startDateTextBox = $('#rest_example_4_start'); var endDateTextBox = $('#rest_example_4_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') ); } });
Get and Set Datetime:
var ex13 = $('#utility_example_1'); ex13.datetimepicker({ dateFormat: "D MM d, yy", separator: ' @ ', ampm: true }); $('#utility_example_1_setdt').click(function(){ ex13.datetimepicker('setDate', (new Date()) ); }); $('#utility_example_1_getdt').click(function(){ alert(ex13.datetimepicker('getDate')); });
Use the utility function to format your own time. $.datepicker.formatTime(format, time, options)
Returns a time string in the specified format.
$('#utility_example_2').text( $.datepicker.formatTime('hh:mm z', { hour: 14, minute: 36, timezone: '+2000' }, { ampm: false }) );
Use the utility function to parses a formatted time. $.datepicker.parseTime(format, timeString, options)
Returns an object with hours, minutes, seconds, milliseconds, timezone.
$('#utility_example_3').text(JSON.stringify( $.datepicker.parseTime('hh:mm:ss:l z', "14:36:21:765 +2000", { ampm: false }) ));