Skip to content

Commit 4ee9097

Browse files
Adds pickerTimeFormat and pickerTimeSuffix options
1 parent 19dfb8c commit 4ee9097

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

index.html

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ <h3>Localization Options</h3>
157157
<dd><em>Default: ['PM', 'P'], A Localization Setting</em> - Array of strings to try and parse against to determine PM.</dd>
158158

159159
<dt>timeFormat</dt>
160-
<dd><em>Default: "hh:mm tt", A Localization Setting</em> - String of format tokens to be replaced with the time. <a href="#tp-formatting" title="Formatting" onclick="$('#tabs').tabs('select',2);">See Formatting</a>.</dd>
160+
<dd><em>Default: "HH:mm", A Localization Setting</em> - String of format tokens to be replaced with the time. <a href="#tp-formatting" title="Formatting" onclick="$('#tabs').tabs('select',2);">See Formatting</a>.</dd>
161161

162162
<dt>timeSuffix</dt>
163163
<dd><em>Default: "", A Localization Setting</em> - String to place after the formatted time.</dd>
@@ -228,6 +228,12 @@ <h3>Time Field Options</h3>
228228
<dt>controlType</dt>
229229
<dd><em>Default: 'slider'</em> - Whether to use 'slider' or 'select'. If 'slider' is unavailable through jQueryUI, 'select' will be used. For advanced usage you may pass an object which implements "create", "options", "value" methods to use controls other than sliders or selects. See the _controls property in the source code for more details.</dd>
230230

231+
<dt>pickerTimeFormat</dt>
232+
<dd><em>Default: (timeFormat option)</em> - How to format the time displayed within the timepicker.</dd>
233+
234+
<dt>pickerTimeSuffix</dt>
235+
<dd><em>Default: (timeSuffix option)</em> - String to place after the formatted time within the timepicker.</dd>
236+
231237
<dt>showHour</dt>
232238
<dd><em>Default: true</em> - Whether to show the hour slider.</dd>
233239

@@ -501,6 +507,7 @@ <h3 id="basic_examples">Basic Initializations</h3>
501507
<pre>
502508
$('#basic_example_3').datetimepicker({
503509
timeFormat: "h:m t",
510+
pickerTimeFormat: "HH:mm",
504511
ampm: true
505512
});
506513
</pre>

jquery-ui-timepicker-addon.js

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@
9898
altAmpm: null,
9999
altSeparator: null,
100100
altTimeSuffix: null,
101+
pickerTimeFormat: null,
102+
pickerTimeSuffix: null,
101103
showTimepicker: true,
102104
timezoneIso8601: false,
103105
timezoneList: null,
@@ -731,10 +733,19 @@
731733
this.ampm = ampm;
732734
}
733735

736+
// Updates the time within the timepicker
734737
this.formattedTime = $.datepicker.formatTime(this._defaults.timeFormat, this, this._defaults);
735738
if (this.$timeObj) {
736-
this.$timeObj.text(this.formattedTime + o.timeSuffix);
739+
var pickerTimeFormat = this._defaults.pickerTimeFormat || this._defaults.timeFormat,
740+
pickerTimeSuffix = this._defaults.pickerTimeSuffix || this._defaults.timeSuffix;
741+
if(pickerTimeFormat === this._defaults.timeFormat){
742+
this.$timeObj.text(this.formattedTime + pickerTimeSuffix);
743+
}
744+
else{
745+
this.$timeObj.text($.datepicker.formatTime(pickerTimeFormat, this, this._defaults) + pickerTimeSuffix);
746+
}
737747
}
748+
738749
this.timeDefined = true;
739750
if (hasChanged) {
740751
this._updateDateTime();
@@ -1167,10 +1178,9 @@
11671178
timezone: '+0000'
11681179
}, time);
11691180

1170-
var tmptime = format;
1171-
var ampmName = options.amNames[0];
1172-
1173-
var hour = parseInt(time.hour, 10);
1181+
var tmptime = format,
1182+
ampmName = options.amNames[0],
1183+
hour = parseInt(time.hour, 10);
11741184
if (options.ampm) {
11751185
if (hour > 11) {
11761186
ampmName = options.pmNames[0];
@@ -1179,13 +1189,13 @@
11791189
tmptime = tmptime.replace(/(?:HH?|hh?|mm?|ss?|[tT]{1,2}|[lz]|('.*?'|".*?"))/g, function(match) {
11801190
switch (match) {
11811191
case 'HH':
1182-
return _convert24to12(hour).slice(-2);
1192+
return ('0' + hour).slice(-2);
11831193
case 'H':
1184-
return _convert24to12(hour);
1194+
return hour;
11851195
case 'hh':
1186-
return ('0' + hour).slice(-2);
1196+
return _convert24to12(hour).slice(-2);
11871197
case 'h':
1188-
return hour;
1198+
return _convert24to12(hour);
11891199
case 'mm':
11901200
return ('0' + time.minute).slice(-2);
11911201
case 'm':
@@ -1198,15 +1208,14 @@
11981208
return ('00' + time.millisec).slice(-3);
11991209
case 'z':
12001210
return time.timezone === null? options.defaultTimezone : time.timezone;
1211+
case 'T':
1212+
return ampmName.charAt(0).toUpperCase();
1213+
case 'TT':
1214+
return ampmName.toUpperCase();
12011215
case 't':
1216+
return ampmName.charAt(0).toLowerCase();
12021217
case 'tt':
1203-
if (options.ampm) {
1204-
if (match.length == 1) {
1205-
ampmName = ampmName.charAt(0);
1206-
}
1207-
return match.charAt(0) === 'T' ? ampmName.toUpperCase() : ampmName.toLowerCase();
1208-
}
1209-
return '';
1218+
return ampmName.toLowerCase();
12101219
default:
12111220
return match.replace(/\'/g, "") || "'";
12121221
}

0 commit comments

Comments
 (0)