Skip to content

Commit 1bb1b4f

Browse files
Fix parsing with new HH format
1 parent b0430eb commit 1bb1b4f

File tree

2 files changed

+25
-34
lines changed

2 files changed

+25
-34
lines changed

index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ <h3>Other Options</h3>
356356

357357
<h2>Formatting Your Time</h2>
358358

359-
<p>The default format is "hh:mm tt", however ampm is turned off by default, so you will see "hh:mm"</p>
359+
<p>The default format is "HH:mm". To use 12 hour time use something similar to: "hh:mm tt".</p>
360360

361361
<dl class="defs">
362362
<dt>H</dt><dd>Hour with no leading 0 (24 hour)</dd>
@@ -380,6 +380,7 @@ <h2>Formatting Your Time</h2>
380380
<ul>
381381
<li>timeFormat option</li>
382382
<li>altTimeFormat option</li>
383+
<li>pickerTimeFormat option</li>
383384
<li>$.datepicker.formatTime(format, timeObj, options) utility method</li>
384385
<li>$.datepicker.parseTime(format, timeStr, options) utility method</li>
385386
</ul>
@@ -501,8 +502,7 @@ <h3 id="basic_examples">Basic Initializations</h3>
501502
</div>
502503
<pre>
503504
$('#basic_example_3').datetimepicker({
504-
timeFormat: "h:m t",
505-
pickerTimeFormat: "HH:mm"
505+
timeFormat: "hh:mm tt"
506506
});
507507
</pre>
508508
</div>

jquery-ui-timepicker-addon.js

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
this.regional[''] = { // Default regional settings
4343
currentText: 'Now',
4444
closeText: 'Done',
45-
//ampm: false,
4645
amNames: ['AM', 'A'],
4746
pmNames: ['PM', 'P'],
4847
timeFormat: 'HH:mm',
@@ -95,7 +94,6 @@
9594
separator: ' ',
9695
altFieldTimeOnly: true,
9796
altTimeFormat: null,
98-
//altAmpm: null,
9997
altSeparator: null,
10098
altTimeSuffix: null,
10199
pickerTimeFormat: null,
@@ -363,7 +361,7 @@
363361
if(litem == 'hour'){
364362
for (var h = o[litem+'Min']; h <= max[litem]; h += parseInt(o[litem+'Grid'], 10)) {
365363
gridSize[litem]++;
366-
var tmph = $.datepicker.formatTime(_useAmpm(o.pickerTimeFormat || o.timeFormat)? 'hht':'HH', {hour:h}, o);
364+
var tmph = $.datepicker.formatTime(useAmpm(o.pickerTimeFormat || o.timeFormat)? 'hht':'HH', {hour:h}, o);
367365
html += '<td data-for="'+litem+'">' + tmph + '</td>';
368366
}
369367
}
@@ -650,7 +648,9 @@
650648
second = (this.second_slider) ? this.control.value(this, this.second_slider, 'second') : false,
651649
millisec = (this.millisec_slider) ? this.control.value(this, this.millisec_slider, 'millisec') : false,
652650
timezone = (this.timezone_select) ? this.timezone_select.val() : false,
653-
o = this._defaults;
651+
o = this._defaults,
652+
pickerTimeFormat = o.pickerTimeFormat || o.timeFormat,
653+
pickerTimeSuffix = o.pickerTimeSuffix || o.timeSuffix;
654654

655655
if (typeof(hour) == 'object') {
656656
hour = false;
@@ -713,20 +713,18 @@
713713

714714
this._limitMinMaxDateTime(this.inst, true);
715715
}
716-
if (o.ampm) {
716+
if (useAmpm(o.timeFormat)) {
717717
this.ampm = ampm;
718718
}
719719

720720
// Updates the time within the timepicker
721-
this.formattedTime = $.datepicker.formatTime(this._defaults.timeFormat, this, this._defaults);
721+
this.formattedTime = $.datepicker.formatTime(o.timeFormat, this, o);
722722
if (this.$timeObj) {
723-
var pickerTimeFormat = this._defaults.pickerTimeFormat || this._defaults.timeFormat,
724-
pickerTimeSuffix = this._defaults.pickerTimeSuffix || this._defaults.timeSuffix;
725-
if(pickerTimeFormat === this._defaults.timeFormat){
723+
if(pickerTimeFormat === o.timeFormat){
726724
this.$timeObj.text(this.formattedTime + pickerTimeSuffix);
727725
}
728726
else{
729-
this.$timeObj.text($.datepicker.formatTime(pickerTimeFormat, this, this._defaults) + pickerTimeSuffix);
727+
this.$timeObj.text($.datepicker.formatTime(pickerTimeFormat, this, o) + pickerTimeSuffix);
730728
}
731729
}
732730

@@ -885,7 +883,7 @@
885883

886884
for(var i=min; i<=max; i+=step){
887885
sel += '<option value="'+ i +'"'+ (i==val? ' selected':'') +'>';
888-
if(unit == 'hour' && _useAmpm(tp_inst._defaults.pickerTimeFormat || tp_inst._defaults.timeFormat))
886+
if(unit == 'hour' && useAmpm(tp_inst._defaults.pickerTimeFormat || tp_inst._defaults.timeFormat))
889887
sel += $.datepicker.formatTime("hh TT", {hour:i}, tp_inst._defaults);
890888
else if(unit == 'millisec' || i >= 10) sel += i;
891889
else sel += '0'+ i.toString();
@@ -1026,7 +1024,7 @@
10261024
var o = extendRemove(extendRemove({}, $.timepicker._defaults), options || {});
10271025

10281026
var regstr = '^' + timeFormat.toString()
1029-
.replace(/(hh?|mm?|ss?|[tT]{1,2}|[lz]|'.*?')/g, function (match) {
1027+
.replace(/([hH]{1,2}|mm?|ss?|[tT]{1,2}|[lz]|'.*?')/g, function (match) {
10301028
switch (match.charAt(0).toLowerCase()) {
10311029
case 'h': return '(\\d?\\d)';
10321030
case 'm': return '(\\d?\\d)';
@@ -1151,9 +1149,9 @@
11511149
case 'H':
11521150
return hour;
11531151
case 'hh':
1154-
return _convert24to12(hour).slice(-2);
1152+
return convert24to12(hour).slice(-2);
11551153
case 'h':
1156-
return _convert24to12(hour);
1154+
return convert24to12(hour);
11571155
case 'mm':
11581156
return ('0' + time.minute).slice(-2);
11591157
case 'm':
@@ -1246,7 +1244,7 @@
12461244

12471245
if (tp_inst) {
12481246
if ($.datepicker._get(inst, 'constrainInput')) {
1249-
var ampm = tp_inst._defaults.ampm,
1247+
var ampm = useAmpm(tp_inst._defaults.timeFormat),
12501248
dateChars = $.datepicker._possibleChars($.datepicker._get(inst, 'dateFormat')),
12511249
datetimeChars = tp_inst._defaults.timeFormat.toString()
12521250
.replace(/[hms]/g, '')
@@ -1584,41 +1582,42 @@
15841582
* jQuery isEmptyObject does not check hasOwnProperty - if someone has added to the object prototype,
15851583
* it will return false for all objects
15861584
*/
1587-
function isEmptyObject (obj) {
1585+
var isEmptyObject = function(obj) {
15881586
var prop;
15891587
for (prop in obj) {
15901588
if (obj.hasOwnProperty(obj)) {
15911589
return false;
15921590
}
15931591
}
15941592
return true;
1595-
}
1593+
};
1594+
15961595
/*
15971596
* jQuery extend now ignores nulls!
15981597
*/
1599-
function extendRemove(target, props) {
1598+
var extendRemove = function(target, props) {
16001599
$.extend(target, props);
16011600
for (var name in props) {
16021601
if (props[name] === null || props[name] === undefined) {
16031602
target[name] = props[name];
16041603
}
16051604
}
16061605
return target;
1607-
}
1606+
};
16081607

16091608
/*
16101609
* Determine by the time format if should use ampm
16111610
* Returns true if should use ampm, false if not
16121611
*/
1613-
var _useAmpm = function(timeFormat){
1612+
var useAmpm = function(timeFormat){
16141613
return (timeFormat.indexOf('t') !== -1 && timeFormat.indexOf('h') !== -1);
1615-
}
1614+
};
16161615

16171616
/*
16181617
* Converts 24 hour format into 12 hour
16191618
* Returns 12 hour with leading 0
16201619
*/
1621-
var _convert24to12 = function(hour) {
1620+
var convert24to12 = function(hour) {
16221621
if (hour > 12) {
16231622
hour = hour - 12;
16241623
}
@@ -1645,18 +1644,11 @@
16451644
// fewer unknowns, mostly numbers and am/pm). We will use the time pattern to split.
16461645
var separator = timeSettings && timeSettings.separator ? timeSettings.separator : $.timepicker._defaults.separator,
16471646
format = timeSettings && timeSettings.timeFormat ? timeSettings.timeFormat : $.timepicker._defaults.timeFormat,
1648-
ampm = timeSettings && timeSettings.ampm ? timeSettings.ampm : $.timepicker._defaults.ampm,
16491647
timeParts = format.split(separator), // how many occurances of separator may be in our format?
16501648
timePartsLen = timeParts.length,
16511649
allParts = dateTimeString.split(separator),
16521650
allPartsLen = allParts.length;
16531651

1654-
// because our default ampm=false, but our default format has tt, we need to filter this out
1655-
if(!ampm){
1656-
timeParts = $.trim(format.replace(/t/gi,'')).split(separator);
1657-
timePartsLen = timeParts.length;
1658-
}
1659-
16601652
if (allPartsLen > 1) {
16611653
return [
16621654
allParts.splice(0,allPartsLen-timePartsLen).join(separator),
@@ -1670,8 +1662,7 @@
16701662
"\ndateTimeString" + dateTimeString +
16711663
"\ndateFormat = " + dateFormat +
16721664
"\nseparator = " + timeSettings.separator +
1673-
"\ntimeFormat = " + timeSettings.timeFormat +
1674-
"\nampm = " + timeSettings.ampm);
1665+
"\ntimeFormat = " + timeSettings.timeFormat);
16751666

16761667
if (err.indexOf(":") >= 0) {
16771668
// Hack! The error message ends with a colon, a space, and

0 commit comments

Comments
 (0)