Create public $.datepicker.formatTime method

This commit is contained in:
Trent Richardson 2012-02-07 10:52:19 -05:00
parent e2faad1db2
commit 27a426e3eb

View file

@ -818,7 +818,8 @@ $.extend(Timepicker.prototype, {
}
if (o.ampm) this.ampm = ampm;
this._formatTime();
//this._formatTime();
this.formattedTime = $.datepicker.formatTime(this._defaults.timeFormat, this, this._defaults);
if (this.$timeObj) this.$timeObj.text(this.formattedTime + o.timeSuffix);
this.timeDefined = true;
if (hasChanged) this._updateDateTime();
@ -837,42 +838,14 @@ $.extend(Timepicker.prototype, {
},
//########################################################################
// format the time all pretty...
// left for any backwards compatibility
//########################################################################
_formatTime: function(time, format, ampm) {
if (ampm == undefined) ampm = this._defaults.ampm;
_formatTime: function(time, format) {
time = time || { hour: this.hour, minute: this.minute, second: this.second, millisec: this.millisec, ampm: this.ampm, timezone: this.timezone };
var tmptime = (format || this._defaults.timeFormat).toString();
var hour = parseInt(time.hour, 10);
if (ampm) {
if (!$.inArray(time.ampm.toUpperCase(), this.amNames) !== -1)
hour = hour % 12;
if (hour === 0)
hour = 12;
}
tmptime = tmptime.replace(/(?:hh?|mm?|ss?|[tT]{1,2}|[lz])/g, function(match) {
switch (match.toLowerCase()) {
case 'hh': return ('0' + hour).slice(-2);
case 'h': return hour;
case 'mm': return ('0' + time.minute).slice(-2);
case 'm': return time.minute;
case 'ss': return ('0' + time.second).slice(-2);
case 's': return time.second;
case 'l': return ('00' + time.millisec).slice(-3);
case 'z': return time.timezone;
case 't': case 'tt':
if (ampm) {
var _ampm = time.ampm;
if (match.length == 1)
_ampm = _ampm.charAt(0);
return match.charAt(0) == 'T' ? _ampm.toUpperCase() : _ampm.toLowerCase();
}
return '';
}
});
tmptime = $.datepicker.formatTime(tmptime, time, this._defaults);
tmptime = $.trim(tmptime);
if (arguments.length) return tmptime;
else this.formattedTime = tmptime;
},
@ -956,6 +929,54 @@ $.fn.extend({
}
});
//########################################################################
// format the time all pretty...
// format = string format of the time
// time = a {}, not a Date() for timezones
// options = essentially the regional[].. amNames, pmNames, ampm
//########################################################################
$.datepicker.formatTime = function(format, time, options) {
options = options || {};
options = $.extend($.timepicker._defaults, options);
time = $.extend({hour:0, minute:0, second:0, millisec:0, timezone:'+0000'}, time);
var tmptime = format;
var ampmName = options['amNames'][0];
var hour = parseInt(time.hour, 10);
if (options.ampm) {
if (hour > 11){
ampmName = options['pmNames'][0];
if(hour > 12)
hour = hour % 12;
}
if (hour === 0)
hour = 12;
}
tmptime = tmptime.replace(/(?:hh?|mm?|ss?|[tT]{1,2}|[lz])/g, function(match) {
switch (match.toLowerCase()) {
case 'hh': return ('0' + hour).slice(-2);
case 'h': return hour;
case 'mm': return ('0' + time.minute).slice(-2);
case 'm': return time.minute;
case 'ss': return ('0' + time.second).slice(-2);
case 's': return time.second;
case 'l': return ('00' + time.millisec).slice(-3);
case 'z': return time.timezone;
case 't': case 'tt':
if (options.ampm) {
if (match.length == 1)
ampmName = ampmName.charAt(0);
return match.charAt(0) == 'T' ? ampmName.toUpperCase() : ampmName.toLowerCase();
}
return '';
}
});
tmptime = $.trim(tmptime);
return tmptime;
}
//########################################################################
// the bad hack :/ override datepicker so it doesnt close on select
// inspired: http://stackoverflow.com/questions/1252512/jquery-datepicker-prevent-closing-picker-when-clicking-a-date/1762378#1762378