Working with Localizations
Timepicker comes with many translations and localizations, thanks to all the contributors. They can be found in the i18n 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 future 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',
amNames: ['AM', 'A'],
pmNames: ['PM', 'P'],
isRTL: 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.
As of version 1.4.5 a combined file of all localizations available is included. This file DOES NOT call setDefaults(), so you will need to pass, or merge with your options.
$('#example123').timepicker($.timepicker.regional['ru']);
Localization files for datepicker are typically available in your jQueryUI downloads.
Examples
Basic Initializations
Using Timezones
Slider Modifications
Create your own control by implementing the create, options, and value methods. If you want to use your new control for all instances use the $.timepicker.setDefaults({controlType:myControl}). Here we implement jQueryUI's spinner control (jQueryUI 1.9+).
var myControl= {
create: function(tp_inst, obj, unit, val, min, max, step){
$('<input class="ui-timepicker-input" value="'+val+'" style="width:50%">')
.appendTo(obj)
.spinner({
min: min,
max: max,
step: step,
change: function(e,ui){ // key events
// don't call if api was used and not key press
if(e.originalEvent !== undefined)
tp_inst._onTimeChange();
tp_inst._onSelectHandler();
},
spin: function(e,ui){ // spin events
tp_inst.control.value(tp_inst, obj, unit, ui.value);
tp_inst._onTimeChange();
tp_inst._onSelectHandler();
}
});
return obj;
},
options: function(tp_inst, obj, unit, opts, val){
if(typeof(opts) == 'string' && val !== undefined)
return obj.find('.ui-timepicker-input').spinner(opts, val);
return obj.find('.ui-timepicker-input').spinner(opts);
},
value: function(tp_inst, obj, unit, val){
if(val !== undefined)
return obj.find('.ui-timepicker-input').spinner('value', val);
return obj.find('.ui-timepicker-input').spinner('value');
}
};
$('#slider_example_5').datetimepicker({
controlType: myControl
});
Alternate Fields
Time Restraints
Time Ranges
Restrict a start and end date by using onSelect and onClose events for more control over functionality:
For more examples and advanced usage grab the Handling Time eBook.
var startDateTextBox = $('#range_example_1_start');
var endDateTextBox = $('#range_example_1_end');
startDateTextBox.datetimepicker({
timeFormat: 'HH:mm z',
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({
timeFormat: 'HH:mm z',
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') );
}
});
Utilities
Use the utility function to format your own time. $.datepicker.formatTime(format, time, options)
- format
- required - string represenation of the time format to use
- time
- required - hash: { hour, minute, second, millisecond, timezone }
- options
- optional - hash of any options in regional translation (ampm, amNames, pmNames..)
Returns a time string in the specified format.
$('#utility_example_2').text(
$.datepicker.formatTime('HH:mm z', { hour: 14, minute: 36, timezone: '+2000' }, {})
);
Use the utility function to parses a formatted time. $.datepicker.parseTime(format, timeString, options)
- format
- required - string represenation of the time format to use
- time
- required - time string matching the format given in parameter 1
- options
- optional - hash of any options in regional translation (ampm, amNames, pmNames..)
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", {})
));