Skip to content

Commit c56e170

Browse files
committed
Changed _formatTime method to optionally take arguments and return a formatted time string
1 parent 749e97b commit c56e170

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

jquery-ui-timepicker-addon.js

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -466,36 +466,34 @@ $.extend(Timepicker.prototype, {
466466
//########################################################################
467467
// format the time all pretty...
468468
//########################################################################
469-
_formatTime: function() {
470-
var tmptime = this._defaults.timeFormat.toString(),
471-
hour12 = ((this.ampm == 'AM') ? (this.hour) : (this.hour % 12));
472-
hour12 = (Number(hour12) === 0) ? 12 : hour12;
473-
474-
if (this._defaults.ampm === true) {
469+
_formatTime: function(time, format, ampm) {
470+
if (ampm == undefined) ampm = this._defaults.ampm;
471+
time = time || { hour: this.hour, minute: this.minute, second: this.second, ampm: this.ampm };
472+
var tmptime = format || this._defaults.timeFormat.toString();
473+
474+
if (ampm) {
475+
var hour12 = ((time.ampm == 'AM') ? (time.hour) : (time.hour % 12));
476+
hour12 = (Number(hour12) === 0) ? 12 : hour12;
475477
tmptime = tmptime.toString()
476478
.replace(/hh/g, ((hour12 < 10) ? '0' : '') + hour12)
477479
.replace(/h/g, hour12)
478-
.replace(/mm/g, ((this.minute < 10) ? '0' : '') + this.minute)
479-
.replace(/m/g, this.minute)
480-
.replace(/ss/g, ((this.second < 10) ? '0' : '') + this.second)
481-
.replace(/s/g, this.second)
482-
.replace(/TT/g, this.ampm.toUpperCase())
483-
.replace(/tt/g, this.ampm.toLowerCase())
484-
.replace(/T/g, this.ampm.charAt(0).toUpperCase())
485-
.replace(/t/g, this.ampm.charAt(0).toLowerCase());
480+
.replace(/TT/g, time.ampm.toUpperCase())
481+
.replace(/tt/g, time.ampm.toLowerCase())
482+
.replace(/T/g, time.ampm.charAt(0).toUpperCase())
483+
.replace(/t/g, time.ampm.charAt(0).toLowerCase());
486484
} else {
487485
tmptime = tmptime.toString()
488-
.replace(/hh/g, ((this.hour < 10) ? '0' : '') + this.hour)
489-
.replace(/h/g, this.hour)
490-
.replace(/mm/g, ((this.minute < 10) ? '0' : '') + this.minute)
491-
.replace(/m/g, this.minute)
492-
.replace(/ss/g, ((this.second < 10) ? '0' : '') + this.second)
493-
.replace(/s/g, this.second);
486+
.replace(/hh/g, ((time.hour < 10) ? '0' : '') + time.hour)
487+
.replace(/h/g, time.hour)
494488
tmptime = $.trim(tmptime.replace(/t/gi, ''));
495489
}
490+
tmptime = tmptime.replace(/mm/g, ((time.minute < 10) ? '0' : '') + time.minute)
491+
.replace(/m/g, time.minute)
492+
.replace(/ss/g, ((time.second < 10) ? '0' : '') + time.second)
493+
.replace(/s/g, time.second);
496494

497-
this.formattedTime = tmptime;
498-
return this.formattedTime;
495+
if (arguments.length) return tmptime;
496+
else this.formattedTime = tmptime;
499497
},
500498

501499
//########################################################################

0 commit comments

Comments
 (0)