Skip to content

Commit 7a5a84a

Browse files
committed
Added support for two digits hour
this should fix jdewit#219 jdewit#209 jdewit#208 jdewit#203, jdewit#209 has tests not yet included here JSBIN http://jsbin.com/xahux/3
1 parent c158912 commit 7a5a84a

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

js/bootstrap-timepicker.js

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
this.timeSeparator = input.data('time-separator') ? input.data('time-separator') : options.timeSeparator;
3434
this.amDesignator = input.data('am') ? input.data('am') : options.amDesignator;
3535
this.pmDesignator = input.data('pm') ? input.data('pm') : options.pmDesignator;
36+
this.twoDigitsHour = input.data('two-digits-hour') ? input.data('two-digits-hour') : options.twoDigitsHour;
3637
this._init();
3738
};
3839

@@ -325,8 +326,13 @@
325326
if (this.hour === '') {
326327
return '';
327328
}
328-
329-
return this.hour + this.timeSeparator + (this.minute.toString().length === 1 ? '0' + this.minute : this.minute) + (this.showSeconds ? this.timeSeparator + (this.second.toString().length === 1 ? '0' + this.second : this.second) : '') + (this.showMeridian ? ' ' + this.meridian : '');
329+
return this.getFormattedHour() + this.timeSeparator + (this.minute.toString().length === 1 ? '0' + this.minute : this.minute) + (this.showSeconds ? this.timeSeparator + (this.second.toString().length === 1 ? '0' + this.second : this.second) : '') + (this.showMeridian ? ' ' + this.meridian : '');
330+
},
331+
332+
getFormattedHour : function() {
333+
var h = "" + this.hour;
334+
if (h.length==1 && this.twoDigitsHour===true) h = '0' + h;
335+
return h;
330336
},
331337

332338
hideWidget: function() {
@@ -437,11 +443,9 @@
437443

438444
if ($element.setSelectionRange) {
439445
setTimeout(function() {
440-
if (self.hour < 10) {
441-
$element.setSelectionRange(0,1);
442-
} else {
443-
$element.setSelectionRange(0,2);
444-
}
446+
var fh = self.getFormattedHour();
447+
$element.setSelectionRange(0,fh.length);
448+
445449
}, 0);
446450
}
447451
},
@@ -454,11 +458,9 @@
454458

455459
if ($element.setSelectionRange) {
456460
setTimeout(function() {
457-
if (self.hour < 10) {
458-
$element.setSelectionRange(2,4);
459-
} else {
460-
$element.setSelectionRange(3,5);
461-
}
461+
var fh = self.getFormattedHour();
462+
var p = 1 + fh.length;
463+
$element.setSelectionRange(p,p+2);
462464
}, 0);
463465
}
464466
},
@@ -471,11 +473,9 @@
471473

472474
if ($element.setSelectionRange) {
473475
setTimeout(function() {
474-
if (self.hour < 10) {
475-
$element.setSelectionRange(5,7);
476-
} else {
477-
$element.setSelectionRange(6,8);
478-
}
476+
var fh = self.getFormattedHour();
477+
var p = 4+fh.length;
478+
$element.setSelectionRange(p,p+2);
479479
}, 0);
480480
}
481481
},
@@ -488,10 +488,11 @@
488488

489489
if ($element.setSelectionRange) {
490490
var start;
491-
if (this.showSeconds) {
492-
start = self.hour < 10 ? 8 : 9;
491+
var fh = self.getFormattedHour();
492+
if (this.showSeconds) {
493+
start = 7 + fh.length;
493494
} else {
494-
start = self.hour < 10 ? 5 : 6;
495+
start = 4 + fh.length;
495496
}
496497

497498
setTimeout(function() {
@@ -731,7 +732,6 @@
731732
this.clear();
732733
return;
733734
}
734-
735735
var timeArray,
736736
hour,
737737
minute,
@@ -1090,6 +1090,7 @@
10901090
template: 'dropdown',
10911091
appendWidgetTo: 'body',
10921092
showWidgetOnAddonClick: true,
1093+
twoDigitsHour : false,
10931094
timeSeparator : ':',
10941095
amDesignator : 'AM',
10951096
pmDesignator : 'PM',

0 commit comments

Comments
 (0)