Skip to content

Commit 17a2dce

Browse files
Remove code using ampm
1 parent 2b52725 commit 17a2dce

File tree

2 files changed

+52
-67
lines changed

2 files changed

+52
-67
lines changed

index.html

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,6 @@ <h3>Alt Field Options</h3>
201201

202202
<dt>altTimeFormat</dt>
203203
<dd><em>Default: (timeFormat option)</em> - The time format to use with the altField.</dd>
204-
205-
<dt>altAmpm</dt>
206-
<dd><em>Default: (ampm option)</em> - Whether or not to use am/pm calculations with the altField.</dd>
207204
</dl>
208205

209206
<h3>Timezone Options</h3>
@@ -509,8 +506,7 @@ <h3 id="basic_examples">Basic Initializations</h3>
509506
<pre>
510507
$('#basic_example_3').datetimepicker({
511508
timeFormat: "h:m t",
512-
pickerTimeFormat: "HH:mm"//,
513-
//ampm: true
509+
pickerTimeFormat: "HH:mm"
514510
});
515511
</pre>
516512
</div>
@@ -584,7 +580,8 @@ <h3 id="slider_examples">Slider Modifications</h3>
584580
<pre>
585581
$('#slider_example_1').timepicker({
586582
hourGrid: 4,
587-
minuteGrid: 10
583+
minuteGrid: 10,
584+
timeFormat: 'hh:mm tt'
588585
});
589586
</pre>
590587
</div>
@@ -628,7 +625,7 @@ <h3 id="slider_examples">Slider Modifications</h3>
628625
<pre>
629626
$('#slider_example_4').datetimepicker({
630627
controlType: 'select',
631-
ampm: true
628+
timeFormat: 'hh:mm tt'
632629
});</pre>
633630
</div>
634631

jquery-ui-timepicker-addon.js

Lines changed: 48 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
ampm: false,
4646
amNames: ['AM', 'A'],
4747
pmNames: ['PM', 'P'],
48-
timeFormat: 'hh:mm tt',
48+
timeFormat: 'HH:mm',
4949
timeSuffix: '',
5050
timeOnlyTitle: 'Choose Time',
5151
timeText: 'Time',
@@ -363,21 +363,7 @@
363363
if(litem == 'hour'){
364364
for (var h = o[litem+'Min']; h <= max[litem]; h += parseInt(o[litem+'Grid'], 10)) {
365365
gridSize[litem]++;
366-
var tmph = (o.ampm && h > 12) ? h - 12 : h;
367-
if (tmph < 10) {
368-
tmph = '0' + tmph;
369-
}
370-
if (o.ampm) {
371-
if (h === 0) {
372-
tmph = 12 + 'a';
373-
} else {
374-
if (h < 12) {
375-
tmph += 'a';
376-
} else {
377-
tmph += 'p';
378-
}
379-
}
380-
}
366+
var tmph = $.datepicker.formatTime(_useAmpm(o.pickerTimeFormat || o.timeFormat)? 'hht':'HH', {hour:h}, o);
381367
html += '<td data-for="'+litem+'">' + tmph + '</td>';
382368
}
383369
}
@@ -426,24 +412,22 @@
426412
}).find("td").click(function(e){
427413
var $t = $(this),
428414
h = $t.html(),
415+
n = parseInt(h.replace(/[^0-9]/g),10),
416+
ap = h.replace(/[^apm]/ig),
429417
f = $t.data('for'); // loses scope, so we use data-for
430418

431-
if (f == 'hour' && o.ampm) {
432-
var ap = h.substring(2).toLowerCase(),
433-
aph = parseInt(h.substring(0, 2), 10);
434-
if (ap == 'a') {
435-
if (aph == 12) {
436-
h = 0;
437-
} else {
438-
h = aph;
419+
if(f == 'hour'){
420+
if(ap.indexOf('p') !== -1 && n < 12){
421+
n += 12;
422+
}
423+
else{
424+
if(ap.indexOf('a') !== -1 && n === 12){
425+
n = 0;
439426
}
440-
} else if (aph == 12) {
441-
h = 12;
442-
} else {
443-
h = aph + 12;
444427
}
445428
}
446-
tp_inst.control.value(tp_inst, tp_inst[f+'_slider'], litem, parseInt(h,10));
429+
430+
tp_inst.control.value(tp_inst, tp_inst[f+'_slider'], litem, n);
447431

448432
tp_inst._onTimeChange();
449433
tp_inst._onSelectHandler();
@@ -802,13 +786,12 @@
802786
this.$input.val(formattedDateTime);
803787
var altFormattedDateTime = '',
804788
altSeparator = this._defaults.altSeparator ? this._defaults.altSeparator : this._defaults.separator,
805-
altTimeSuffix = this._defaults.altTimeSuffix ? this._defaults.altTimeSuffix : this._defaults.timeSuffix,
806-
altOpts = $.extend({}, this._defaults, { ampm: (this._defaults.altAmpm !== null ? this._defaults.altAmpm : this._defaults.ampm) });
789+
altTimeSuffix = this._defaults.altTimeSuffix ? this._defaults.altTimeSuffix : this._defaults.timeSuffix;
807790

808791
if (this._defaults.altFormat) altFormattedDateTime = $.datepicker.formatDate(this._defaults.altFormat, (dt === null ? new Date() : dt), formatCfg);
809792
else altFormattedDateTime = this.formattedDate;
810793
if (altFormattedDateTime) altFormattedDateTime += altSeparator;
811-
if (this._defaults.altTimeFormat) altFormattedDateTime += $.datepicker.formatTime(this._defaults.altTimeFormat, this, altOpts) + altTimeSuffix;
794+
if (this._defaults.altTimeFormat) altFormattedDateTime += $.datepicker.formatTime(this._defaults.altTimeFormat, this, this._defaults) + altTimeSuffix;
812795
else altFormattedDateTime += this.formattedTime + altTimeSuffix;
813796
this.$altInput.val(altFormattedDateTime);
814797
} else {
@@ -902,13 +885,8 @@
902885

903886
for(var i=min; i<=max; i+=step){
904887
sel += '<option value="'+ i +'"'+ (i==val? ' selected':'') +'>';
905-
if(unit == 'hour' && tp_inst._defaults.ampm){
906-
m = i%12;
907-
if(i === 0 || i === 12) sel += '12';
908-
else if(m < 10) sel += '0'+ m.toString();
909-
else sel += m;
910-
sel += ' '+ ((i < 12)? tp_inst._defaults.amNames[0] : tp_inst._defaults.pmNames[0])[ul]();
911-
}
888+
if(unit == 'hour' && _useAmpm(tp_inst._defaults.pickerTimeFormat || tp_inst._defaults.timeFormat))
889+
sel += $.datepicker.formatTime("hh TT", {hour:i}, tp_inst._defaults);
912890
else if(unit == 'millisec' || i >= 10) sel += i;
913891
else sel += '0'+ i.toString();
914892
sel += '</option>';
@@ -1141,22 +1119,6 @@
11411119
return false;
11421120
};
11431121

1144-
_convert24to12 = function(hour) {
1145-
if (hour > 12) {
1146-
hour = hour - 12;
1147-
}
1148-
1149-
if (hour == 0) {
1150-
hour = 12;
1151-
}
1152-
1153-
if (hour < 10) {
1154-
hour = "0" + hour;
1155-
}
1156-
1157-
return String(hour);
1158-
};
1159-
11601122
/*
11611123
* Public utility to format the time
11621124
* format = string format of the time
@@ -1177,7 +1139,7 @@
11771139
var tmptime = format,
11781140
ampmName = options.amNames[0],
11791141
hour = parseInt(time.hour, 10);
1180-
1142+
11811143
if (hour > 11) {
11821144
ampmName = options.pmNames[0];
11831145
}
@@ -1323,11 +1285,9 @@
13231285
altFormattedDateTime = '',
13241286
altSeparator = tp_inst._defaults.altSeparator ? tp_inst._defaults.altSeparator : tp_inst._defaults.separator,
13251287
altTimeSuffix = tp_inst._defaults.altTimeSuffix ? tp_inst._defaults.altTimeSuffix : tp_inst._defaults.timeSuffix,
1326-
altTimeFormat = tp_inst._defaults.altTimeFormat !== null ? tp_inst._defaults.altTimeFormat : tp_inst._defaults.timeFormat,
1327-
altAmpm = tp_inst._defaults.altAmpm !== null ? tp_inst._defaults.altAmpm : tp_inst._defaults.ampm,
1328-
altOpts = $.extend({}, tp_inst._defaults, { ampm: altAmpm });
1288+
altTimeFormat = tp_inst._defaults.altTimeFormat !== null ? tp_inst._defaults.altTimeFormat : tp_inst._defaults.timeFormat;
13291289

1330-
altFormattedDateTime += $.datepicker.formatTime(altTimeFormat, tp_inst, altOpts) + altTimeSuffix;
1290+
altFormattedDateTime += $.datepicker.formatTime(altTimeFormat, tp_inst, tp_inst._defaults) + altTimeSuffix;
13311291
if(!tp_inst._defaults.timeOnly && !tp_inst._defaults.altFieldTimeOnly){
13321292
if(tp_inst._defaults.altFormat)
13331293
altFormattedDateTime = $.datepicker.formatDate(tp_inst._defaults.altFormat, (date === null ? new Date() : date), formatCfg) + altSeparator + altFormattedDateTime;
@@ -1646,6 +1606,34 @@
16461606
return target;
16471607
}
16481608

1609+
/*
1610+
* Determine by the time format if should use ampm
1611+
* Returns true if should use ampm, false if not
1612+
*/
1613+
var _useAmpm = function(timeFormat){
1614+
return (timeFormat.indexOf('t') !== -1 && timeFormat.indexOf('h') !== -1);
1615+
}
1616+
1617+
/*
1618+
* Converts 24 hour format into 12 hour
1619+
* Returns 12 hour with leading 0
1620+
*/
1621+
var _convert24to12 = function(hour) {
1622+
if (hour > 12) {
1623+
hour = hour - 12;
1624+
}
1625+
1626+
if (hour == 0) {
1627+
hour = 12;
1628+
}
1629+
1630+
if (hour < 10) {
1631+
hour = "0" + hour;
1632+
}
1633+
1634+
return String(hour);
1635+
};
1636+
16491637
/*
16501638
* Splits datetime string into date ans time substrings.
16511639
* Throws exception when date can't be parsed

0 commit comments

Comments
 (0)