Skip to content

Commit 27a426e

Browse files
Create public $.datepicker.formatTime method
1 parent e2faad1 commit 27a426e

File tree

1 file changed

+54
-33
lines changed

1 file changed

+54
-33
lines changed

jquery-ui-timepicker-addon.js

Lines changed: 54 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,8 @@ $.extend(Timepicker.prototype, {
818818
}
819819
if (o.ampm) this.ampm = ampm;
820820

821-
this._formatTime();
821+
//this._formatTime();
822+
this.formattedTime = $.datepicker.formatTime(this._defaults.timeFormat, this, this._defaults);
822823
if (this.$timeObj) this.$timeObj.text(this.formattedTime + o.timeSuffix);
823824
this.timeDefined = true;
824825
if (hasChanged) this._updateDateTime();
@@ -837,42 +838,14 @@ $.extend(Timepicker.prototype, {
837838
},
838839

839840
//########################################################################
840-
// format the time all pretty...
841+
// left for any backwards compatibility
841842
//########################################################################
842-
_formatTime: function(time, format, ampm) {
843-
if (ampm == undefined) ampm = this._defaults.ampm;
843+
_formatTime: function(time, format) {
844844
time = time || { hour: this.hour, minute: this.minute, second: this.second, millisec: this.millisec, ampm: this.ampm, timezone: this.timezone };
845845
var tmptime = (format || this._defaults.timeFormat).toString();
846846

847-
var hour = parseInt(time.hour, 10);
848-
if (ampm) {
849-
if (!$.inArray(time.ampm.toUpperCase(), this.amNames) !== -1)
850-
hour = hour % 12;
851-
if (hour === 0)
852-
hour = 12;
853-
}
854-
tmptime = tmptime.replace(/(?:hh?|mm?|ss?|[tT]{1,2}|[lz])/g, function(match) {
855-
switch (match.toLowerCase()) {
856-
case 'hh': return ('0' + hour).slice(-2);
857-
case 'h': return hour;
858-
case 'mm': return ('0' + time.minute).slice(-2);
859-
case 'm': return time.minute;
860-
case 'ss': return ('0' + time.second).slice(-2);
861-
case 's': return time.second;
862-
case 'l': return ('00' + time.millisec).slice(-3);
863-
case 'z': return time.timezone;
864-
case 't': case 'tt':
865-
if (ampm) {
866-
var _ampm = time.ampm;
867-
if (match.length == 1)
868-
_ampm = _ampm.charAt(0);
869-
return match.charAt(0) == 'T' ? _ampm.toUpperCase() : _ampm.toLowerCase();
870-
}
871-
return '';
872-
}
873-
});
874-
875-
tmptime = $.trim(tmptime);
847+
tmptime = $.datepicker.formatTime(tmptime, time, this._defaults);
848+
876849
if (arguments.length) return tmptime;
877850
else this.formattedTime = tmptime;
878851
},
@@ -956,6 +929,54 @@ $.fn.extend({
956929
}
957930
});
958931

932+
//########################################################################
933+
// format the time all pretty...
934+
// format = string format of the time
935+
// time = a {}, not a Date() for timezones
936+
// options = essentially the regional[].. amNames, pmNames, ampm
937+
//########################################################################
938+
$.datepicker.formatTime = function(format, time, options) {
939+
options = options || {};
940+
options = $.extend($.timepicker._defaults, options);
941+
time = $.extend({hour:0, minute:0, second:0, millisec:0, timezone:'+0000'}, time);
942+
943+
var tmptime = format;
944+
var ampmName = options['amNames'][0];
945+
946+
var hour = parseInt(time.hour, 10);
947+
if (options.ampm) {
948+
if (hour > 11){
949+
ampmName = options['pmNames'][0];
950+
if(hour > 12)
951+
hour = hour % 12;
952+
}
953+
if (hour === 0)
954+
hour = 12;
955+
}
956+
tmptime = tmptime.replace(/(?:hh?|mm?|ss?|[tT]{1,2}|[lz])/g, function(match) {
957+
switch (match.toLowerCase()) {
958+
case 'hh': return ('0' + hour).slice(-2);
959+
case 'h': return hour;
960+
case 'mm': return ('0' + time.minute).slice(-2);
961+
case 'm': return time.minute;
962+
case 'ss': return ('0' + time.second).slice(-2);
963+
case 's': return time.second;
964+
case 'l': return ('00' + time.millisec).slice(-3);
965+
case 'z': return time.timezone;
966+
case 't': case 'tt':
967+
if (options.ampm) {
968+
if (match.length == 1)
969+
ampmName = ampmName.charAt(0);
970+
return match.charAt(0) == 'T' ? ampmName.toUpperCase() : ampmName.toLowerCase();
971+
}
972+
return '';
973+
}
974+
});
975+
976+
tmptime = $.trim(tmptime);
977+
return tmptime;
978+
}
979+
959980
//########################################################################
960981
// the bad hack :/ override datepicker so it doesnt close on select
961982
// inspired: http://stackoverflow.com/questions/1252512/jquery-datepicker-prevent-closing-picker-when-clicking-a-date/1762378#1762378

0 commit comments

Comments
 (0)