2 * jQuery timepicker addon
3 * By: Trent Richardson [http://trentrichardson.com]
5 * Last Modified: 02/05/2012
7 * Copyright 2012 Trent Richardson
8 * Dual licensed under the MIT and GPL licenses.
9 * http://trentrichardson.com/Impromptu/GPL-LICENSE.txt
10 * http://trentrichardson.com/Impromptu/MIT-LICENSE.txt
13 * .ui-timepicker-div .ui-widget-header { margin-bottom: 8px; }
14 * .ui-timepicker-div dl { text-align: left; }
15 * .ui-timepicker-div dl dt { height: 25px; margin-bottom: -25px; }
16 * .ui-timepicker-div dl dd { margin: 0 10px 10px 65px; }
17 * .ui-timepicker-div td { font-size: 90%; }
18 * .ui-tpicker-grid-label { background: none; border: none; margin: 0; padding: 0; }
23 // Prevent "Uncaught RangeError: Maximum call stack size exceeded"
24 $.ui.timepicker = $.ui.timepicker || {};
25 if ($.ui.timepicker.version) {
29 $.extend($.ui, { timepicker: { version: "1.0.0" } });
31 /* Time picker manager.
32 Use the singleton instance of this class, $.timepicker, to interact with the time picker.
33 Settings for (groups of) time pickers are maintained in an instance object,
34 allowing multiple different settings on the same page. */
36 function Timepicker() {
37 this.regional = []; // Available regional settings, indexed by language code
38 this.regional[''] = { // Default regional settings
44 timeFormat: 'hh:mm tt',
46 timeOnlyTitle: 'Choose Time',
51 millisecText: 'Millisecond',
52 timezoneText: 'Time Zone'
54 this._defaults = { // Global defaults for all the datetime picker instances
55 showButtonPanel: true,
89 altFieldTimeOnly: true,
91 timezoneIso8609: false,
93 addSliderAccess: false,
94 sliderAccessArgs: null
96 $.extend(this._defaults, this.regional['']);
99 $.extend(Timepicker.prototype, {
107 millisec_slider: null,
108 timezone_select: null,
114 hourMinOriginal: null,
115 minuteMinOriginal: null,
116 secondMinOriginal: null,
117 millisecMinOriginal: null,
118 hourMaxOriginal: null,
119 minuteMaxOriginal: null,
120 secondMaxOriginal: null,
121 millisecMaxOriginal: null,
125 formattedDateTime: '',
128 /* Override the default settings for all instances of the time picker.
129 @param settings object - the new settings to use as defaults (anonymous object)
130 @return the manager object */
131 setDefaults: function(settings) {
132 extendRemove(this._defaults, settings || {});
136 //########################################################################
137 // Create a new Timepicker instance
138 //########################################################################
139 _newInst: function($input, o) {
140 var tp_inst = new Timepicker(),
143 for (var attrName in this._defaults) {
144 var attrValue = $input.attr('time:' + attrName);
147 inlineSettings[attrName] = eval(attrValue);
149 inlineSettings[attrName] = attrValue;
153 tp_inst._defaults = $.extend({}, this._defaults, inlineSettings, o, {
154 beforeShow: function(input, dp_inst) {
155 if ($.isFunction(o.beforeShow))
156 return o.beforeShow(input, dp_inst, tp_inst);
158 onChangeMonthYear: function(year, month, dp_inst) {
159 // Update the time as well : this prevents the time from disappearing from the $input field.
160 tp_inst._updateDateTime(dp_inst);
161 if ($.isFunction(o.onChangeMonthYear))
162 o.onChangeMonthYear.call($input[0], year, month, dp_inst, tp_inst);
164 onClose: function(dateText, dp_inst) {
165 if (tp_inst.timeDefined === true && $input.val() != '')
166 tp_inst._updateDateTime(dp_inst);
167 if ($.isFunction(o.onClose))
168 o.onClose.call($input[0], dateText, dp_inst, tp_inst);
170 timepicker: tp_inst // add timepicker as a property of datepicker: $.datepicker._get(dp_inst, 'timepicker');
172 tp_inst.amNames = $.map(tp_inst._defaults.amNames, function(val) { return val.toUpperCase(); });
173 tp_inst.pmNames = $.map(tp_inst._defaults.pmNames, function(val) { return val.toUpperCase(); });
175 if (tp_inst._defaults.timezoneList === null) {
176 var timezoneList = [];
177 for (var i = -11; i <= 12; i++)
178 timezoneList.push((i >= 0 ? '+' : '-') + ('0' + Math.abs(i).toString()).slice(-2) + '00');
179 if (tp_inst._defaults.timezoneIso8609)
180 timezoneList = $.map(timezoneList, function(val) {
181 return val == '+0000' ? 'Z' : (val.substring(0, 3) + ':' + val.substring(3));
183 tp_inst._defaults.timezoneList = timezoneList;
186 tp_inst.hour = tp_inst._defaults.hour;
187 tp_inst.minute = tp_inst._defaults.minute;
188 tp_inst.second = tp_inst._defaults.second;
189 tp_inst.millisec = tp_inst._defaults.millisec;
191 tp_inst.$input = $input;
194 tp_inst.$altInput = $(o.altField)
195 .css({ cursor: 'pointer' })
196 .focus(function(){ $input.trigger("focus"); });
198 if(tp_inst._defaults.minDate==0 || tp_inst._defaults.minDateTime==0)
200 tp_inst._defaults.minDate=new Date();
202 if(tp_inst._defaults.maxDate==0 || tp_inst._defaults.maxDateTime==0)
204 tp_inst._defaults.maxDate=new Date();
207 // datepicker needs minDate/maxDate, timepicker needs minDateTime/maxDateTime..
208 if(tp_inst._defaults.minDate !== undefined && tp_inst._defaults.minDate instanceof Date)
209 tp_inst._defaults.minDateTime = new Date(tp_inst._defaults.minDate.getTime());
210 if(tp_inst._defaults.minDateTime !== undefined && tp_inst._defaults.minDateTime instanceof Date)
211 tp_inst._defaults.minDate = new Date(tp_inst._defaults.minDateTime.getTime());
212 if(tp_inst._defaults.maxDate !== undefined && tp_inst._defaults.maxDate instanceof Date)
213 tp_inst._defaults.maxDateTime = new Date(tp_inst._defaults.maxDate.getTime());
214 if(tp_inst._defaults.maxDateTime !== undefined && tp_inst._defaults.maxDateTime instanceof Date)
215 tp_inst._defaults.maxDate = new Date(tp_inst._defaults.maxDateTime.getTime());
219 //########################################################################
220 // add our sliders to the calendar
221 //########################################################################
222 _addTimePicker: function(dp_inst) {
223 var currDT = (this.$altInput && this._defaults.altFieldTimeOnly) ?
224 this.$input.val() + ' ' + this.$altInput.val() :
227 this.timeDefined = this._parseTime(currDT);
228 this._limitMinMaxDateTime(dp_inst, false);
229 this._injectTimePicker();
232 //########################################################################
233 // parse the time string from input value or _setTime
234 //########################################################################
235 _parseTime: function(timeString, withDate) {
236 var regstr = this._defaults.timeFormat.toString()
237 .replace(/h{1,2}/ig, '(\\d?\\d)')
238 .replace(/m{1,2}/ig, '(\\d?\\d)')
239 .replace(/s{1,2}/ig, '(\\d?\\d)')
240 .replace(/l{1}/ig, '(\\d?\\d?\\d)')
241 .replace(/t{1,2}/ig, this._getPatternAmpm())
242 .replace(/z{1}/ig, '(z|[-+]\\d\\d:?\\d\\d)?')
243 .replace(/\s/g, '\\s?') + this._defaults.timeSuffix + '$',
244 order = this._getFormatPositions(),
248 if (!this.inst) this.inst = $.datepicker._getInst(this.$input[0]);
250 if (withDate || !this._defaults.timeOnly) {
251 // the time should come after x number of characters and a space.
252 // x = at least the length of text specified by the date format
253 var dp_dateFormat = $.datepicker._get(this.inst, 'dateFormat');
254 // escape special regex characters in the seperator
255 var specials = new RegExp("[.*+?|()\\[\\]{}\\\\]", "g");
256 regstr = '^.{' + dp_dateFormat.length + ',}?' + this._defaults.separator.replace(specials, "\\$&") + regstr;
259 treg = timeString.match(new RegExp(regstr, 'i'));
262 if (order.t !== -1) {
263 if (treg[order.t] === undefined || treg[order.t].length === 0) {
267 ampm = $.inArray(treg[order.t].toUpperCase(), this.amNames) !== -1 ? 'AM' : 'PM';
268 this.ampm = this._defaults[ampm == 'AM' ? 'amNames' : 'pmNames'][0];
272 if (order.h !== -1) {
273 if (ampm == 'AM' && treg[order.h] == '12')
274 this.hour = 0; // 12am = 0 hour
275 else if (ampm == 'PM' && treg[order.h] != '12')
276 this.hour = (parseFloat(treg[order.h]) + 12).toFixed(0); // 12pm = 12 hour, any other pm = hour + 12
277 else this.hour = Number(treg[order.h]);
280 if (order.m !== -1) this.minute = Number(treg[order.m]);
281 if (order.s !== -1) this.second = Number(treg[order.s]);
282 if (order.l !== -1) this.millisec = Number(treg[order.l]);
283 if (order.z !== -1 && treg[order.z] !== undefined) {
284 var tz = treg[order.z].toUpperCase();
287 tz = this._defaults.timezoneIso8609 ? 'Z' : '+0000';
290 if (this._defaults.timezoneIso8609)
291 tz = tz.substring(1) == '0000'
293 : tz.substring(0, 3) + ':' + tz.substring(3);
296 if (!this._defaults.timezoneIso8609)
297 tz = tz == 'Z' || tz.substring(1) == '00:00'
299 : tz.replace(/:/, '');
300 else if (tz.substring(1) == '00:00')
313 //########################################################################
314 // pattern for standard and localized AM/PM markers
315 //########################################################################
316 _getPatternAmpm: function() {
320 $.merge(markers, o.amNames);
322 $.merge(markers, o.pmNames);
323 markers = $.map(markers, function(val) { return val.replace(/[.*+?|()\[\]{}\\]/g, '\\$&'); });
324 return '(' + markers.join('|') + ')?';
327 //########################################################################
328 // figure out position of time elements.. cause js cant do named captures
329 //########################################################################
330 _getFormatPositions: function() {
331 var finds = this._defaults.timeFormat.toLowerCase().match(/(h{1,2}|m{1,2}|s{1,2}|l{1}|t{1,2}|z)/g),
332 orders = { h: -1, m: -1, s: -1, l: -1, t: -1, z: -1 };
335 for (var i = 0; i < finds.length; i++)
336 if (orders[finds[i].toString().charAt(0)] == -1)
337 orders[finds[i].toString().charAt(0)] = i + 1;
342 //########################################################################
343 // generate and inject html for timepicker into ui datepicker
344 //########################################################################
345 _injectTimePicker: function() {
346 var $dp = this.inst.dpDiv,
349 // Added by Peter Medeiros:
350 // - Figure out what the hour/minute/second max should be based on the step values.
351 // - Example: if stepMinute is 15, then minMax is 45.
352 hourMax = parseInt((o.hourMax - ((o.hourMax - o.hourMin) % o.stepHour)) ,10),
353 minMax = parseInt((o.minuteMax - ((o.minuteMax - o.minuteMin) % o.stepMinute)) ,10),
354 secMax = parseInt((o.secondMax - ((o.secondMax - o.secondMin) % o.stepSecond)) ,10),
355 millisecMax = parseInt((o.millisecMax - ((o.millisecMax - o.millisecMin) % o.stepMillisec)) ,10),
356 dp_id = this.inst.id.toString().replace(/([^A-Za-z0-9_])/g, '');
358 // Prevent displaying twice
359 //if ($dp.find("div#ui-timepicker-div-"+ dp_id).length === 0) {
360 if ($dp.find("div#ui-timepicker-div-"+ dp_id).length === 0 && o.showTimepicker) {
361 var noDisplay = ' style="display:none;"',
362 html = '<div class="ui-timepicker-div" id="ui-timepicker-div-' + dp_id + '"><dl>' +
363 '<dt class="ui_tpicker_time_label" id="ui_tpicker_time_label_' + dp_id + '"' +
364 ((o.showTime) ? '' : noDisplay) + '>' + o.timeText + '</dt>' +
365 '<dd class="ui_tpicker_time" id="ui_tpicker_time_' + dp_id + '"' +
366 ((o.showTime) ? '' : noDisplay) + '></dd>' +
367 '<dt class="ui_tpicker_hour_label" id="ui_tpicker_hour_label_' + dp_id + '"' +
368 ((o.showHour) ? '' : noDisplay) + '>' + o.hourText + '</dt>',
372 millisecGridSize = 0,
376 html += '<dd class="ui_tpicker_hour"><div id="ui_tpicker_hour_' + dp_id + '"' +
377 ((o.showHour) ? '' : noDisplay) + '></div>';
378 if (o.showHour && o.hourGrid > 0) {
379 html += '<div style="padding-left: 1px"><table class="ui-tpicker-grid-label"><tr>';
381 for (var h = o.hourMin; h <= hourMax; h += parseInt(o.hourGrid,10)) {
383 var tmph = (o.ampm && h > 12) ? h-12 : h;
384 if (tmph < 10) tmph = '0' + tmph;
386 if (h == 0) tmph = 12 +'a';
387 else if (h < 12) tmph += 'a';
390 html += '<td>' + tmph + '</td>';
393 html += '</tr></table></div>';
398 html += '<dt class="ui_tpicker_minute_label" id="ui_tpicker_minute_label_' + dp_id + '"' +
399 ((o.showMinute) ? '' : noDisplay) + '>' + o.minuteText + '</dt>'+
400 '<dd class="ui_tpicker_minute"><div id="ui_tpicker_minute_' + dp_id + '"' +
401 ((o.showMinute) ? '' : noDisplay) + '></div>';
403 if (o.showMinute && o.minuteGrid > 0) {
404 html += '<div style="padding-left: 1px"><table class="ui-tpicker-grid-label"><tr>';
406 for (var m = o.minuteMin; m <= minMax; m += parseInt(o.minuteGrid,10)) {
408 html += '<td>' + ((m < 10) ? '0' : '') + m + '</td>';
411 html += '</tr></table></div>';
416 html += '<dt class="ui_tpicker_second_label" id="ui_tpicker_second_label_' + dp_id + '"' +
417 ((o.showSecond) ? '' : noDisplay) + '>' + o.secondText + '</dt>'+
418 '<dd class="ui_tpicker_second"><div id="ui_tpicker_second_' + dp_id + '"'+
419 ((o.showSecond) ? '' : noDisplay) + '></div>';
421 if (o.showSecond && o.secondGrid > 0) {
422 html += '<div style="padding-left: 1px"><table><tr>';
424 for (var s = o.secondMin; s <= secMax; s += parseInt(o.secondGrid,10)) {
426 html += '<td>' + ((s < 10) ? '0' : '') + s + '</td>';
429 html += '</tr></table></div>';
434 html += '<dt class="ui_tpicker_millisec_label" id="ui_tpicker_millisec_label_' + dp_id + '"' +
435 ((o.showMillisec) ? '' : noDisplay) + '>' + o.millisecText + '</dt>'+
436 '<dd class="ui_tpicker_millisec"><div id="ui_tpicker_millisec_' + dp_id + '"'+
437 ((o.showMillisec) ? '' : noDisplay) + '></div>';
439 if (o.showMillisec && o.millisecGrid > 0) {
440 html += '<div style="padding-left: 1px"><table><tr>';
442 for (var l = o.millisecMin; l <= millisecMax; l += parseInt(o.millisecGrid,10)) {
444 html += '<td>' + ((l < 10) ? '0' : '') + l + '</td>';
447 html += '</tr></table></div>';
452 html += '<dt class="ui_tpicker_timezone_label" id="ui_tpicker_timezone_label_' + dp_id + '"' +
453 ((o.showTimezone) ? '' : noDisplay) + '>' + o.timezoneText + '</dt>';
454 html += '<dd class="ui_tpicker_timezone" id="ui_tpicker_timezone_' + dp_id + '"' +
455 ((o.showTimezone) ? '' : noDisplay) + '></dd>';
457 html += '</dl></div>';
460 // if we only want time picker...
461 if (o.timeOnly === true) {
463 '<div class="ui-widget-header ui-helper-clearfix ui-corner-all">' +
464 '<div class="ui-datepicker-title">' + o.timeOnlyTitle + '</div>' +
466 $dp.find('.ui-datepicker-header, .ui-datepicker-calendar').hide();
469 this.hour_slider = $tp.find('#ui_tpicker_hour_'+ dp_id).slider({
470 orientation: "horizontal",
475 slide: function(event, ui) {
476 tp_inst.hour_slider.slider( "option", "value", ui.value);
477 tp_inst._onTimeChange();
482 // Updated by Peter Medeiros:
483 // - Pass in Event and UI instance into slide function
484 this.minute_slider = $tp.find('#ui_tpicker_minute_'+ dp_id).slider({
485 orientation: "horizontal",
490 slide: function(event, ui) {
491 tp_inst.minute_slider.slider( "option", "value", ui.value);
492 tp_inst._onTimeChange();
496 this.second_slider = $tp.find('#ui_tpicker_second_'+ dp_id).slider({
497 orientation: "horizontal",
502 slide: function(event, ui) {
503 tp_inst.second_slider.slider( "option", "value", ui.value);
504 tp_inst._onTimeChange();
508 this.millisec_slider = $tp.find('#ui_tpicker_millisec_'+ dp_id).slider({
509 orientation: "horizontal",
510 value: this.millisec,
513 step: o.stepMillisec,
514 slide: function(event, ui) {
515 tp_inst.millisec_slider.slider( "option", "value", ui.value);
516 tp_inst._onTimeChange();
520 this.timezone_select = $tp.find('#ui_tpicker_timezone_'+ dp_id).append('<select></select>').find("select");
521 $.fn.append.apply(this.timezone_select,
522 $.map(o.timezoneList, function(val, idx) {
523 return $("<option />")
524 .val(typeof val == "object" ? val.value : val)
525 .text(typeof val == "object" ? val.label : val);
528 this.timezone_select.val((typeof this.timezone != "undefined" && this.timezone != null && this.timezone != "") ? this.timezone : o.timezone);
529 this.timezone_select.change(function() {
530 tp_inst._onTimeChange();
533 // Add grid functionality
534 if (o.showHour && o.hourGrid > 0) {
535 size = 100 * hourGridSize * o.hourGrid / (hourMax - o.hourMin);
537 $tp.find(".ui_tpicker_hour table").css({
539 marginLeft: (size / (-2 * hourGridSize)) + "%",
540 borderCollapse: 'collapse'
541 }).find("td").each( function(index) {
542 $(this).click(function() {
543 var h = $(this).html();
545 var ap = h.substring(2).toLowerCase(),
546 aph = parseInt(h.substring(0,2), 10);
548 if (aph == 12) h = 0;
550 } else if (aph == 12) h = 12;
553 tp_inst.hour_slider.slider("option", "value", h);
554 tp_inst._onTimeChange();
555 tp_inst._onSelectHandler();
558 width: (100 / hourGridSize) + '%',
565 if (o.showMinute && o.minuteGrid > 0) {
566 size = 100 * minuteGridSize * o.minuteGrid / (minMax - o.minuteMin);
567 $tp.find(".ui_tpicker_minute table").css({
569 marginLeft: (size / (-2 * minuteGridSize)) + "%",
570 borderCollapse: 'collapse'
571 }).find("td").each(function(index) {
572 $(this).click(function() {
573 tp_inst.minute_slider.slider("option", "value", $(this).html());
574 tp_inst._onTimeChange();
575 tp_inst._onSelectHandler();
578 width: (100 / minuteGridSize) + '%',
585 if (o.showSecond && o.secondGrid > 0) {
586 $tp.find(".ui_tpicker_second table").css({
588 marginLeft: (size / (-2 * secondGridSize)) + "%",
589 borderCollapse: 'collapse'
590 }).find("td").each(function(index) {
591 $(this).click(function() {
592 tp_inst.second_slider.slider("option", "value", $(this).html());
593 tp_inst._onTimeChange();
594 tp_inst._onSelectHandler();
597 width: (100 / secondGridSize) + '%',
604 if (o.showMillisec && o.millisecGrid > 0) {
605 $tp.find(".ui_tpicker_millisec table").css({
607 marginLeft: (size / (-2 * millisecGridSize)) + "%",
608 borderCollapse: 'collapse'
609 }).find("td").each(function(index) {
610 $(this).click(function() {
611 tp_inst.millisec_slider.slider("option", "value", $(this).html());
612 tp_inst._onTimeChange();
613 tp_inst._onSelectHandler();
616 width: (100 / millisecGridSize) + '%',
623 var $buttonPanel = $dp.find('.ui-datepicker-buttonpane');
624 if ($buttonPanel.length) $buttonPanel.before($tp);
625 else $dp.append($tp);
627 this.$timeObj = $tp.find('#ui_tpicker_time_'+ dp_id);
629 if (this.inst !== null) {
630 var timeDefined = this.timeDefined;
631 this._onTimeChange();
632 this.timeDefined = timeDefined;
635 //Emulate datepicker onSelect behavior. Call on slidestop.
636 var onSelectDelegate = function() {
637 tp_inst._onSelectHandler();
639 this.hour_slider.bind('slidestop',onSelectDelegate);
640 this.minute_slider.bind('slidestop',onSelectDelegate);
641 this.second_slider.bind('slidestop',onSelectDelegate);
642 this.millisec_slider.bind('slidestop',onSelectDelegate);
644 // slideAccess integration: http://trentrichardson.com/2011/11/11/jquery-ui-sliders-and-touch-accessibility/
645 if (this._defaults.addSliderAccess){
646 var sliderAccessArgs = this._defaults.sliderAccessArgs;
647 setTimeout(function(){ // fix for inline mode
648 if($tp.find('.ui-slider-access').length == 0){
649 $tp.find('.ui-slider:visible').sliderAccess(sliderAccessArgs);
651 // fix any grids since sliders are shorter
652 var sliderAccessWidth = $tp.find('.ui-slider-access:eq(0)').outerWidth(true);
653 if(sliderAccessWidth){
654 $tp.find('table:visible').each(function(){
656 oldWidth = $g.outerWidth(),
657 oldMarginLeft = $g.css('marginLeft').toString().replace('%',''),
658 newWidth = oldWidth - sliderAccessWidth,
659 newMarginLeft = ((oldMarginLeft * newWidth)/oldWidth) + '%';
661 $g.css({ width: newWidth, marginLeft: newMarginLeft });
667 // end slideAccess integration
672 //########################################################################
673 // This function tries to limit the ability to go outside the
674 // min/max date range
675 //########################################################################
676 _limitMinMaxDateTime: function(dp_inst, adjustSliders){
677 var o = this._defaults,
678 dp_date = new Date(dp_inst.selectedYear, dp_inst.selectedMonth, dp_inst.selectedDay);
680 if(!this._defaults.showTimepicker) return; // No time so nothing to check here
682 if($.datepicker._get(dp_inst, 'minDateTime') !== null && $.datepicker._get(dp_inst, 'minDateTime') !== undefined && dp_date){
683 var minDateTime = $.datepicker._get(dp_inst, 'minDateTime'),
684 minDateTimeDate = new Date(minDateTime.getFullYear(), minDateTime.getMonth(), minDateTime.getDate(), 0, 0, 0, 0);
686 if(this.hourMinOriginal === null || this.minuteMinOriginal === null || this.secondMinOriginal === null || this.millisecMinOriginal === null){
687 this.hourMinOriginal = o.hourMin;
688 this.minuteMinOriginal = o.minuteMin;
689 this.secondMinOriginal = o.secondMin;
690 this.millisecMinOriginal = o.millisecMin;
693 if(dp_inst.settings.timeOnly || minDateTimeDate.getTime() == dp_date.getTime()) {
694 this._defaults.hourMin = minDateTime.getHours();
695 if (this.hour <= this._defaults.hourMin) {
696 this.hour = this._defaults.hourMin;
697 this._defaults.minuteMin = minDateTime.getMinutes();
698 if (this.minute <= this._defaults.minuteMin) {
699 this.minute = this._defaults.minuteMin;
700 this._defaults.secondMin = minDateTime.getSeconds();
701 } else if (this.second <= this._defaults.secondMin){
702 this.second = this._defaults.secondMin;
703 this._defaults.millisecMin = minDateTime.getMilliseconds();
705 if(this.millisec < this._defaults.millisecMin)
706 this.millisec = this._defaults.millisecMin;
707 this._defaults.millisecMin = this.millisecMinOriginal;
710 this._defaults.minuteMin = this.minuteMinOriginal;
711 this._defaults.secondMin = this.secondMinOriginal;
712 this._defaults.millisecMin = this.millisecMinOriginal;
715 this._defaults.hourMin = this.hourMinOriginal;
716 this._defaults.minuteMin = this.minuteMinOriginal;
717 this._defaults.secondMin = this.secondMinOriginal;
718 this._defaults.millisecMin = this.millisecMinOriginal;
722 if($.datepicker._get(dp_inst, 'maxDateTime') !== null && $.datepicker._get(dp_inst, 'maxDateTime') !== undefined && dp_date){
723 var maxDateTime = $.datepicker._get(dp_inst, 'maxDateTime'),
724 maxDateTimeDate = new Date(maxDateTime.getFullYear(), maxDateTime.getMonth(), maxDateTime.getDate(), 0, 0, 0, 0);
726 if(this.hourMaxOriginal === null || this.minuteMaxOriginal === null || this.secondMaxOriginal === null){
727 this.hourMaxOriginal = o.hourMax;
728 this.minuteMaxOriginal = o.minuteMax;
729 this.secondMaxOriginal = o.secondMax;
730 this.millisecMaxOriginal = o.millisecMax;
733 if(dp_inst.settings.timeOnly || maxDateTimeDate.getTime() == dp_date.getTime()){
734 this._defaults.hourMax = maxDateTime.getHours();
735 if (this.hour >= this._defaults.hourMax) {
736 this.hour = this._defaults.hourMax;
737 this._defaults.minuteMax = maxDateTime.getMinutes();
738 if (this.minute >= this._defaults.minuteMax) {
739 this.minute = this._defaults.minuteMax;
740 this._defaults.secondMax = maxDateTime.getSeconds();
741 } else if (this.second >= this._defaults.secondMax) {
742 this.second = this._defaults.secondMax;
743 this._defaults.millisecMax = maxDateTime.getMilliseconds();
745 if(this.millisec > this._defaults.millisecMax) this.millisec = this._defaults.millisecMax;
746 this._defaults.millisecMax = this.millisecMaxOriginal;
749 this._defaults.minuteMax = this.minuteMaxOriginal;
750 this._defaults.secondMax = this.secondMaxOriginal;
751 this._defaults.millisecMax = this.millisecMaxOriginal;
754 this._defaults.hourMax = this.hourMaxOriginal;
755 this._defaults.minuteMax = this.minuteMaxOriginal;
756 this._defaults.secondMax = this.secondMaxOriginal;
757 this._defaults.millisecMax = this.millisecMaxOriginal;
761 if(adjustSliders !== undefined && adjustSliders === true){
762 var hourMax = parseInt((this._defaults.hourMax - ((this._defaults.hourMax - this._defaults.hourMin) % this._defaults.stepHour)) ,10),
763 minMax = parseInt((this._defaults.minuteMax - ((this._defaults.minuteMax - this._defaults.minuteMin) % this._defaults.stepMinute)) ,10),
764 secMax = parseInt((this._defaults.secondMax - ((this._defaults.secondMax - this._defaults.secondMin) % this._defaults.stepSecond)) ,10),
765 millisecMax = parseInt((this._defaults.millisecMax - ((this._defaults.millisecMax - this._defaults.millisecMin) % this._defaults.stepMillisec)) ,10);
768 this.hour_slider.slider("option", { min: this._defaults.hourMin, max: hourMax }).slider('value', this.hour);
769 if(this.minute_slider)
770 this.minute_slider.slider("option", { min: this._defaults.minuteMin, max: minMax }).slider('value', this.minute);
771 if(this.second_slider)
772 this.second_slider.slider("option", { min: this._defaults.secondMin, max: secMax }).slider('value', this.second);
773 if(this.millisec_slider)
774 this.millisec_slider.slider("option", { min: this._defaults.millisecMin, max: millisecMax }).slider('value', this.millisec);
780 //########################################################################
781 // when a slider moves, set the internal time...
782 // on time change is also called when the time is updated in the text field
783 //########################################################################
784 _onTimeChange: function() {
785 var hour = (this.hour_slider) ? this.hour_slider.slider('value') : false,
786 minute = (this.minute_slider) ? this.minute_slider.slider('value') : false,
787 second = (this.second_slider) ? this.second_slider.slider('value') : false,
788 millisec = (this.millisec_slider) ? this.millisec_slider.slider('value') : false,
789 timezone = (this.timezone_select) ? this.timezone_select.val() : false,
792 if (typeof(hour) == 'object') hour = false;
793 if (typeof(minute) == 'object') minute = false;
794 if (typeof(second) == 'object') second = false;
795 if (typeof(millisec) == 'object') millisec = false;
796 if (typeof(timezone) == 'object') timezone = false;
798 if (hour !== false) hour = parseInt(hour,10);
799 if (minute !== false) minute = parseInt(minute,10);
800 if (second !== false) second = parseInt(second,10);
801 if (millisec !== false) millisec = parseInt(millisec,10);
803 var ampm = o[hour < 12 ? 'amNames' : 'pmNames'][0];
805 // If the update was done in the input field, the input field should not be updated.
806 // If the update was done using the sliders, update the input field.
807 var hasChanged = (hour != this.hour || minute != this.minute
808 || second != this.second || millisec != this.millisec
809 || (this.ampm.length > 0
810 && (hour < 12) != ($.inArray(this.ampm.toUpperCase(), this.amNames) !== -1))
811 || timezone != this.timezone);
815 if (hour !== false)this.hour = hour;
816 if (minute !== false) this.minute = minute;
817 if (second !== false) this.second = second;
818 if (millisec !== false) this.millisec = millisec;
819 if (timezone !== false) this.timezone = timezone;
821 if (!this.inst) this.inst = $.datepicker._getInst(this.$input[0]);
823 this._limitMinMaxDateTime(this.inst, true);
825 if (o.ampm) this.ampm = ampm;
827 //this._formatTime();
828 this.formattedTime = $.datepicker.formatTime(this._defaults.timeFormat, this, this._defaults);
829 if (this.$timeObj) this.$timeObj.text(this.formattedTime + o.timeSuffix);
830 this.timeDefined = true;
831 if (hasChanged) this._updateDateTime();
834 //########################################################################
835 // call custom onSelect.
836 // bind to sliders slidestop, and grid click.
837 //########################################################################
838 _onSelectHandler: function() {
839 var onSelect = this._defaults.onSelect;
840 var inputEl = this.$input ? this.$input[0] : null;
841 if (onSelect && inputEl) {
842 onSelect.apply(inputEl, [this.formattedDateTime, this]);
846 //########################################################################
847 // left for any backwards compatibility
848 //########################################################################
849 _formatTime: function(time, format) {
850 time = time || { hour: this.hour, minute: this.minute, second: this.second, millisec: this.millisec, ampm: this.ampm, timezone: this.timezone };
851 var tmptime = (format || this._defaults.timeFormat).toString();
853 tmptime = $.datepicker.formatTime(tmptime, time, this._defaults);
855 if (arguments.length) return tmptime;
856 else this.formattedTime = tmptime;
859 //########################################################################
860 // update our input with the new date time..
861 //########################################################################
862 _updateDateTime: function(dp_inst) {
863 dp_inst = this.inst || dp_inst;
864 var dt = $.datepicker._daylightSavingAdjust(new Date(dp_inst.selectedYear, dp_inst.selectedMonth, dp_inst.selectedDay)),
865 dateFmt = $.datepicker._get(dp_inst, 'dateFormat'),
866 formatCfg = $.datepicker._getFormatConfig(dp_inst),
867 timeAvailable = dt !== null && this.timeDefined;
868 this.formattedDate = $.datepicker.formatDate(dateFmt, (dt === null ? new Date() : dt), formatCfg);
869 var formattedDateTime = this.formattedDate;
870 if (dp_inst.lastVal !== undefined && (dp_inst.lastVal.length > 0 && this.$input.val().length === 0))
873 if (this._defaults.timeOnly === true) {
874 formattedDateTime = this.formattedTime;
875 } else if (this._defaults.timeOnly !== true && (this._defaults.alwaysSetTime || timeAvailable)) {
876 formattedDateTime += this._defaults.separator + this.formattedTime + this._defaults.timeSuffix;
879 this.formattedDateTime = formattedDateTime;
881 if(!this._defaults.showTimepicker) {
882 this.$input.val(this.formattedDate);
883 } else if (this.$altInput && this._defaults.altFieldTimeOnly === true) {
884 this.$altInput.val(this.formattedTime);
885 this.$input.val(this.formattedDate);
886 } else if(this.$altInput) {
887 this.$altInput.val(formattedDateTime);
888 this.$input.val(formattedDateTime);
890 this.$input.val(formattedDateTime);
893 this.$input.trigger("change");
899 //########################################################################
900 // shorthand just to use timepicker..
901 //########################################################################
902 timepicker: function(o) {
904 var tmp_args = arguments;
906 if (typeof o == 'object') tmp_args[0] = $.extend(o, { timeOnly: true });
908 return $(this).each(function() {
909 $.fn.datetimepicker.apply($(this), tmp_args);
913 //########################################################################
914 // extend timepicker to datepicker
915 //########################################################################
916 datetimepicker: function(o) {
918 tmp_args = arguments;
920 if (typeof(o) == 'string'){
922 return $.fn.datepicker.apply($(this[0]), tmp_args);
924 return this.each(function() {
926 $t.datepicker.apply($t, tmp_args);
930 return this.each(function() {
932 $t.datepicker($.timepicker._newInst($t, o)._defaults);
937 //########################################################################
938 // format the time all pretty...
939 // format = string format of the time
940 // time = a {}, not a Date() for timezones
941 // options = essentially the regional[].. amNames, pmNames, ampm
942 //########################################################################
943 $.datepicker.formatTime = function(format, time, options) {
944 options = options || {};
945 options = $.extend($.timepicker._defaults, options);
946 time = $.extend({hour:0, minute:0, second:0, millisec:0, timezone:'+0000'}, time);
948 var tmptime = format;
949 var ampmName = options['amNames'][0];
951 var hour = parseInt(time.hour, 10);
954 ampmName = options['pmNames'][0];
961 tmptime = tmptime.replace(/(?:hh?|mm?|ss?|[tT]{1,2}|[lz])/g, function(match) {
962 switch (match.toLowerCase()) {
963 case 'hh': return ('0' + hour).slice(-2);
964 case 'h': return hour;
965 case 'mm': return ('0' + time.minute).slice(-2);
966 case 'm': return time.minute;
967 case 'ss': return ('0' + time.second).slice(-2);
968 case 's': return time.second;
969 case 'l': return ('00' + time.millisec).slice(-3);
970 case 'z': return time.timezone;
973 if (match.length == 1)
974 ampmName = ampmName.charAt(0);
975 return match.charAt(0) == 'T' ? ampmName.toUpperCase() : ampmName.toLowerCase();
981 tmptime = $.trim(tmptime);
985 //########################################################################
986 // the bad hack :/ override datepicker so it doesnt close on select
987 // inspired: http://stackoverflow.com/questions/1252512/jquery-datepicker-prevent-closing-picker-when-clicking-a-date/1762378#1762378
988 //########################################################################
989 $.datepicker._base_selectDate = $.datepicker._selectDate;
990 $.datepicker._selectDate = function (id, dateStr) {
991 var inst = this._getInst($(id)[0]),
992 tp_inst = this._get(inst, 'timepicker');
995 tp_inst._limitMinMaxDateTime(inst, true);
996 inst.inline = inst.stay_open = true;
997 //This way the onSelect handler called from calendarpicker get the full dateTime
998 this._base_selectDate(id, dateStr);
999 inst.inline = inst.stay_open = false;
1000 this._notifyChange(inst);
1001 this._updateDatepicker(inst);
1003 else this._base_selectDate(id, dateStr);
1006 //#############################################################################################
1007 // second bad hack :/ override datepicker so it triggers an event when changing the input field
1008 // and does not redraw the datepicker on every selectDate event
1009 //#############################################################################################
1010 $.datepicker._base_updateDatepicker = $.datepicker._updateDatepicker;
1011 $.datepicker._updateDatepicker = function(inst) {
1013 // don't popup the datepicker if there is another instance already opened
1014 var input = inst.input[0];
1015 if($.datepicker._curInst &&
1016 $.datepicker._curInst != inst &&
1017 $.datepicker._datepickerShowing &&
1018 $.datepicker._lastInput != input) {
1022 if (typeof(inst.stay_open) !== 'boolean' || inst.stay_open === false) {
1024 this._base_updateDatepicker(inst);
1026 // Reload the time control when changing something in the input text field.
1027 var tp_inst = this._get(inst, 'timepicker');
1028 if(tp_inst) tp_inst._addTimePicker(inst);
1032 //#######################################################################################
1033 // third bad hack :/ override datepicker so it allows spaces and colon in the input field
1034 //#######################################################################################
1035 $.datepicker._base_doKeyPress = $.datepicker._doKeyPress;
1036 $.datepicker._doKeyPress = function(event) {
1037 var inst = $.datepicker._getInst(event.target),
1038 tp_inst = $.datepicker._get(inst, 'timepicker');
1041 if ($.datepicker._get(inst, 'constrainInput')) {
1042 var ampm = tp_inst._defaults.ampm,
1043 dateChars = $.datepicker._possibleChars($.datepicker._get(inst, 'dateFormat')),
1044 datetimeChars = tp_inst._defaults.timeFormat.toString()
1045 .replace(/[hms]/g, '')
1046 .replace(/TT/g, ampm ? 'APM' : '')
1047 .replace(/Tt/g, ampm ? 'AaPpMm' : '')
1048 .replace(/tT/g, ampm ? 'AaPpMm' : '')
1049 .replace(/T/g, ampm ? 'AP' : '')
1050 .replace(/tt/g, ampm ? 'apm' : '')
1051 .replace(/t/g, ampm ? 'ap' : '') +
1053 tp_inst._defaults.separator +
1054 tp_inst._defaults.timeSuffix +
1055 (tp_inst._defaults.showTimezone ? tp_inst._defaults.timezoneList.join('') : '') +
1056 (tp_inst._defaults.amNames.join('')) +
1057 (tp_inst._defaults.pmNames.join('')) +
1059 chr = String.fromCharCode(event.charCode === undefined ? event.keyCode : event.charCode);
1060 return event.ctrlKey || (chr < ' ' || !dateChars || datetimeChars.indexOf(chr) > -1);
1064 return $.datepicker._base_doKeyPress(event);
1067 //#######################################################################################
1068 // Override key up event to sync manual input changes.
1069 //#######################################################################################
1070 $.datepicker._base_doKeyUp = $.datepicker._doKeyUp;
1071 $.datepicker._doKeyUp = function (event) {
1072 var inst = $.datepicker._getInst(event.target),
1073 tp_inst = $.datepicker._get(inst, 'timepicker');
1076 if (tp_inst._defaults.timeOnly && (inst.input.val() != inst.lastVal)) {
1078 $.datepicker._updateDatepicker(inst);
1081 $.datepicker.log(err);
1086 return $.datepicker._base_doKeyUp(event);
1089 //#######################################################################################
1090 // override "Today" button to also grab the time.
1091 //#######################################################################################
1092 $.datepicker._base_gotoToday = $.datepicker._gotoToday;
1093 $.datepicker._gotoToday = function(id) {
1094 var inst = this._getInst($(id)[0]),
1096 this._base_gotoToday(id);
1097 var now = new Date();
1098 var tp_inst = this._get(inst, 'timepicker');
1099 if (tp_inst && tp_inst._defaults.showTimezone && tp_inst.timezone_select) {
1100 var tzoffset = now.getTimezoneOffset(); // If +0100, returns -60
1101 var tzsign = tzoffset > 0 ? '-' : '+';
1102 tzoffset = Math.abs(tzoffset);
1103 var tzmin = tzoffset % 60;
1104 tzoffset = tzsign + ('0' + (tzoffset - tzmin) / 60).slice(-2) + ('0' + tzmin).slice(-2);
1105 if (tp_inst._defaults.timezoneIso8609)
1106 tzoffset = tzoffset.substring(0, 3) + ':' + tzoffset.substring(3);
1107 tp_inst.timezone_select.val(tzoffset);
1109 this._setTime(inst, now);
1110 $( '.ui-datepicker-today', $dp).click();
1113 //#######################################################################################
1114 // Disable & enable the Time in the datetimepicker
1115 //#######################################################################################
1116 $.datepicker._disableTimepickerDatepicker = function(target, date, withDate) {
1117 var inst = this._getInst(target),
1118 tp_inst = this._get(inst, 'timepicker');
1119 $(target).datepicker('getDate'); // Init selected[Year|Month|Day]
1121 tp_inst._defaults.showTimepicker = false;
1122 tp_inst._updateDateTime(inst);
1126 $.datepicker._enableTimepickerDatepicker = function(target, date, withDate) {
1127 var inst = this._getInst(target),
1128 tp_inst = this._get(inst, 'timepicker');
1129 $(target).datepicker('getDate'); // Init selected[Year|Month|Day]
1131 tp_inst._defaults.showTimepicker = true;
1132 tp_inst._addTimePicker(inst); // Could be disabled on page load
1133 tp_inst._updateDateTime(inst);
1137 //#######################################################################################
1138 // Create our own set time function
1139 //#######################################################################################
1140 $.datepicker._setTime = function(inst, date) {
1141 var tp_inst = this._get(inst, 'timepicker');
1143 var defaults = tp_inst._defaults,
1144 // calling _setTime with no date sets time to defaults
1145 hour = date ? date.getHours() : defaults.hour,
1146 minute = date ? date.getMinutes() : defaults.minute,
1147 second = date ? date.getSeconds() : defaults.second,
1148 millisec = date ? date.getMilliseconds() : defaults.millisec;
1150 //check if within min/max times..
1151 if ((hour < defaults.hourMin || hour > defaults.hourMax) || (minute < defaults.minuteMin || minute > defaults.minuteMax) || (second < defaults.secondMin || second > defaults.secondMax) || (millisec < defaults.millisecMin || millisec > defaults.millisecMax)) {
1152 hour = defaults.hourMin;
1153 minute = defaults.minuteMin;
1154 second = defaults.secondMin;
1155 millisec = defaults.millisecMin;
1158 tp_inst.hour = hour;
1159 tp_inst.minute = minute;
1160 tp_inst.second = second;
1161 tp_inst.millisec = millisec;
1163 if (tp_inst.hour_slider) tp_inst.hour_slider.slider('value', hour);
1164 if (tp_inst.minute_slider) tp_inst.minute_slider.slider('value', minute);
1165 if (tp_inst.second_slider) tp_inst.second_slider.slider('value', second);
1166 if (tp_inst.millisec_slider) tp_inst.millisec_slider.slider('value', millisec);
1168 tp_inst._onTimeChange();
1169 tp_inst._updateDateTime(inst);
1173 //#######################################################################################
1174 // Create new public method to set only time, callable as $().datepicker('setTime', date)
1175 //#######################################################################################
1176 $.datepicker._setTimeDatepicker = function(target, date, withDate) {
1177 var inst = this._getInst(target),
1178 tp_inst = this._get(inst, 'timepicker');
1181 this._setDateFromField(inst);
1184 if (typeof date == "string") {
1185 tp_inst._parseTime(date, withDate);
1186 tp_date = new Date();
1187 tp_date.setHours(tp_inst.hour, tp_inst.minute, tp_inst.second, tp_inst.millisec);
1189 else tp_date = new Date(date.getTime());
1190 if (tp_date.toString() == 'Invalid Date') tp_date = undefined;
1191 this._setTime(inst, tp_date);
1197 //#######################################################################################
1198 // override setDate() to allow setting time too within Date object
1199 //#######################################################################################
1200 $.datepicker._base_setDateDatepicker = $.datepicker._setDateDatepicker;
1201 $.datepicker._setDateDatepicker = function(target, date) {
1202 var inst = this._getInst(target),
1203 tp_date = (date instanceof Date) ? new Date(date.getTime()) : date;
1205 this._updateDatepicker(inst);
1206 this._base_setDateDatepicker.apply(this, arguments);
1207 this._setTimeDatepicker(target, tp_date, true);
1210 //#######################################################################################
1211 // override getDate() to allow getting time too within Date object
1212 //#######################################################################################
1213 $.datepicker._base_getDateDatepicker = $.datepicker._getDateDatepicker;
1214 $.datepicker._getDateDatepicker = function(target, noDefault) {
1215 var inst = this._getInst(target),
1216 tp_inst = this._get(inst, 'timepicker');
1219 this._setDateFromField(inst, noDefault);
1220 var date = this._getDate(inst);
1221 if (date && tp_inst._parseTime($(target).val(), tp_inst.timeOnly)) date.setHours(tp_inst.hour, tp_inst.minute, tp_inst.second, tp_inst.millisec);
1224 return this._base_getDateDatepicker(target, noDefault);
1227 //#######################################################################################
1228 // override parseDate() because UI 1.8.14 throws an error about "Extra characters"
1229 // An option in datapicker to ignore extra format characters would be nicer.
1230 //#######################################################################################
1231 $.datepicker._base_parseDate = $.datepicker.parseDate;
1232 $.datepicker.parseDate = function(format, value, settings) {
1235 date = this._base_parseDate(format, value, settings);
1237 if (err.indexOf(":") >= 0) {
1238 // Hack! The error message ends with a colon, a space, and
1239 // the "extra" characters. We rely on that instead of
1240 // attempting to perfectly reproduce the parsing algorithm.
1241 date = this._base_parseDate(format, value.substring(0,value.length-(err.length-err.indexOf(':')-2)), settings);
1243 // The underlying error was not related to the time
1250 //#######################################################################################
1251 // override formatDate to set date with time to the input
1252 //#######################################################################################
1253 $.datepicker._base_formatDate = $.datepicker._formatDate;
1254 $.datepicker._formatDate = function(inst, day, month, year){
1255 var tp_inst = this._get(inst, 'timepicker');
1257 tp_inst._updateDateTime(inst);
1258 return tp_inst.$input.val();
1260 return this._base_formatDate(inst);
1263 //#######################################################################################
1264 // override options setter to add time to maxDate(Time) and minDate(Time). MaxDate
1265 //#######################################################################################
1266 $.datepicker._base_optionDatepicker = $.datepicker._optionDatepicker;
1267 $.datepicker._optionDatepicker = function(target, name, value) {
1268 var inst = this._getInst(target),
1269 tp_inst = this._get(inst, 'timepicker');
1271 var min = null, max = null, onselect = null;
1272 if (typeof name == 'string') { // if min/max was set with the string
1273 if (name === 'minDate' || name === 'minDateTime' )
1275 else if (name === 'maxDate' || name === 'maxDateTime')
1277 else if (name === 'onSelect')
1279 } else if (typeof name == 'object') { //if min/max was set with the JSON
1282 else if (name.minDateTime)
1283 min = name.minDateTime;
1284 else if (name.maxDate)
1286 else if (name.maxDateTime)
1287 max = name.maxDateTime;
1289 if(min) { //if min was set
1293 min = new Date(min);
1295 tp_inst._defaults.minDate = min;
1296 tp_inst._defaults.minDateTime = min;
1297 } else if (max) { //if max was set
1302 tp_inst._defaults.maxDate = max;
1303 tp_inst._defaults.maxDateTime = max;
1304 } else if (onselect)
1305 tp_inst._defaults.onSelect = onselect;
1307 if (value === undefined)
1308 return this._base_optionDatepicker(target, name);
1309 return this._base_optionDatepicker(target, name, value);
1312 //#######################################################################################
1313 // jQuery extend now ignores nulls!
1314 //#######################################################################################
1315 function extendRemove(target, props) {
1316 $.extend(target, props);
1317 for (var name in props)
1318 if (props[name] === null || props[name] === undefined)
1319 target[name] = props[name];
1323 $.timepicker = new Timepicker(); // singleton instance
1324 $.timepicker.version = "1.0.0";