Skip to content

Commit bcdd398

Browse files
author
Philipp Eichhorn
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 see: eleumik@7a5a84a
1 parent 18000b5 commit bcdd398

1 file changed

Lines changed: 29 additions & 33 deletions

File tree

js/bootstrap-timepicker.js

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
this.appendWidgetTo = options.appendWidgetTo;
3232
this.showWidgetOnAddonClick = options.showWidgetOnAddonClick;
3333
this.maxHours = options.maxHours;
34-
this.explicitMode = options.explicitMode; // If true 123 = 1:23, 12345 = 1:23:45, else invalid.
34+
this.explicitMode = options.explicitMode; // If true 123 = 1:23, 12345 = 1:23:45, else invalid.
35+
this.twoDigitsHour = options.twoDigitsHour;
3536

3637
this.handleDocumentClick = function (e) {
3738
var self = e.data.scope;
@@ -351,7 +352,15 @@
351352
return '';
352353
}
353354

354-
return this.hour + ':' + (this.minute.toString().length === 1 ? '0' + this.minute : this.minute) + (this.showSeconds ? ':' + (this.second.toString().length === 1 ? '0' + this.second : this.second) : '') + (this.showMeridian ? ' ' + this.meridian : '');
355+
return this.getFormattedHour() + ':' + (this.minute.toString().length === 1 ? '0' + this.minute : this.minute) + (this.showSeconds ? ':' + (this.second.toString().length === 1 ? '0' + this.second : this.second) : '') + (this.showMeridian ? ' ' + this.meridian : '');
356+
},
357+
358+
getFormattedHour : function() {
359+
var h = '' + this.hour;
360+
if (h.length === 1 && this.twoDigitsHour) {
361+
h = '0' + h;
362+
}
363+
return h;
355364
},
356365

357366
hideWidget: function() {
@@ -462,11 +471,8 @@
462471

463472
if ($element.setSelectionRange) {
464473
setTimeout(function() {
465-
if (self.hour < 10) {
466-
$element.setSelectionRange(0,1);
467-
} else {
468-
$element.setSelectionRange(0,2);
469-
}
474+
var fh = self.getFormattedHour();
475+
$element.setSelectionRange(0,fh.length);
470476
}, 0);
471477
}
472478
},
@@ -479,11 +485,9 @@
479485

480486
if ($element.setSelectionRange) {
481487
setTimeout(function() {
482-
if (self.hour < 10) {
483-
$element.setSelectionRange(2,4);
484-
} else {
485-
$element.setSelectionRange(3,5);
486-
}
488+
var fh = self.getFormattedHour();
489+
var p = 1 + fh.length;
490+
$element.setSelectionRange(p,p+2);
487491
}, 0);
488492
}
489493
},
@@ -496,11 +500,9 @@
496500

497501
if ($element.setSelectionRange) {
498502
setTimeout(function() {
499-
if (self.hour < 10) {
500-
$element.setSelectionRange(5,7);
501-
} else {
502-
$element.setSelectionRange(6,8);
503-
}
503+
var fh = self.getFormattedHour();
504+
var p = 4 + fh.length;
505+
$element.setSelectionRange(p,p+2);
504506
}, 0);
505507
}
506508
},
@@ -512,23 +514,16 @@
512514
this.highlightedUnit = 'meridian';
513515

514516
if ($element.setSelectionRange) {
517+
var start;
518+
var fh = self.getFormattedHour();
515519
if (this.showSeconds) {
516-
setTimeout(function() {
517-
if (self.hour < 10) {
518-
$element.setSelectionRange(8,10);
519-
} else {
520-
$element.setSelectionRange(9,11);
521-
}
522-
}, 0);
520+
start = 7 + fh.length;
523521
} else {
524-
setTimeout(function() {
525-
if (self.hour < 10) {
526-
$element.setSelectionRange(5,7);
527-
} else {
528-
$element.setSelectionRange(6,8);
529-
}
530-
}, 0);
522+
start = 4 + fh.length;
531523
}
524+
setTimeout(function() {
525+
$element.setSelectionRange(start,start+self.meridian.length);
526+
}, 0);
532527
}
533528
},
534529

@@ -988,7 +983,7 @@
988983
return;
989984
}
990985

991-
var hour = this.hour,
986+
var hour = this.getFormattedHour(),
992987
minute = this.minute.toString().length === 1 ? '0' + this.minute : this.minute,
993988
second = this.second.toString().length === 1 ? '0' + this.second : this.second;
994989

@@ -1147,7 +1142,8 @@
11471142
appendWidgetTo: 'body',
11481143
showWidgetOnAddonClick: true,
11491144
maxHours: 24,
1150-
explicitMode: false
1145+
explicitMode: false,
1146+
twoDigitsHour : false
11511147
};
11521148

11531149
$.fn.timepicker.Constructor = Timepicker;

0 commit comments

Comments
 (0)