Timepicker to jQuery UI Datepicker

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

Download

Download Timepicker Addon

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; }
.ui-timepicker-div dl dd{ margin: -25px 0 10px 65px; }
.ui-timepicker-div td { font-size: 90%; }

Donation

Has this Timepicker Addon been helpful to you?

Some Examples

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")

h
Hour with no leading 0
hh
Hour with leading 0
m
Minute with no leading 0
mm
Minute with leading 0
s
Second with no leading 0
ss
Second with leading 0
l
Milliseconds always with leading 0
t
a or p for AM/PM
T
A or P for AM/PM
tt
am or pm for AM/PM
TT
AM or PM for AM/PM

Also related to your timeFormat, but not required, is the separator option

separator
Place holder between date and time, default=" "
$('#example4').datetimepicker({
	timeFormat: 'h:m',
	separator: ' @ '
});

Show Seconds, Minutes, or Hours

showHour
Show the hour, default=true
showMinute
Show the minute, default=true
showSecond
Show the second, default=false
showMillisec
Show the millisecond, default=false
$('#example5').datetimepicker({
	showSecond: true,
	showMillisec: true,
	timeFormat: 'hh:mm:ss:l'
});

Hours, Minutes, Seconds in steps

stepHour
hour steps, default=0.05 (for smooth, 1 would jump to each hour)
stepMinute
minute steps, default=0.05
stepSecond
second steps, default=0.05
stepMillisec
second steps, default=0.5
$('#example6').datetimepicker({
	showSecond: true,
	timeFormat: 'hh:mm:ss',
	stepHour: 2,
	stepMinute: 10,
	stepSecond: 10
});

Default Hours, Minutes, Seconds

hour
default=0
minute
default=0
second
default=0
millisec
default=0
$('#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.

hourMin
default=0
hourMax
default=23
minuteMin
default=0
minuteMax
default=59
secondMin
default=0
secondMax
default=59
millisecMin
default=0
millisecMax
default=999
$('#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:

timeOnlyTitle
default="Choose Time"
timeText
default="Time"
hourText
default="Hour"
minuteText
default="Minute"
secondText
default="Second"
millisecText
default="Milliecond"
currentText
default="Now"
closeText
default="Done"
ampm
default=false (true/false, Some regions do use this, some don’t)

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:

hourGrid
default=0
minuteGrid
default=0
secondGrid
default=0
millisecGrid
default=0

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:

Set Datetime Get 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({
	onSelect: function (selectedDateTime){
		var start = $(this).datetimepicker('getDate');
		$('#example16_end').datetimepicker('option', 'minDate', new Date(start.getTime()) );
	}
});

$('#example16_end').datetimepicker({
	onSelect: function (selectedDateTime){
		var end = $(this).datetimepicker('getDate');
		$('#example16_start').datetimepicker('option', 'maxDate', new Date(end.getTime()) );
	}
});

Version

Version 0.9.7

Last updated on 10/02/2011

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.

GPL License

MIT License

http://trentrichardson.com/examples/timepicker/

Leave a comment