Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 24h display when showMeridian is false. #209

Open
wants to merge 1 commit into
base: gh-pages
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions js/bootstrap-timepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,28 @@
},

getTime: function() {
var timestr;
if (this.hour === '') {
return '';
}

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 : '');
timestr = this.paddedTime(this.hour) + ':' + this.paddedTime(this.minute, true);
if (this.showSeconds) {
timestr = timestr + ':' + this.paddedTime(this.second, true);
}
if (this.showMeridian) {
timestr = timestr + ' ' + this.meridian;
}
return timestr;
},

paddedTime: function(val, override) {
if (!this.showMeridian || override) {
// ensure that given value is padded with zeros
return ('0' + val).slice(-2);
} else {
return val;
}
},

hideWidget: function() {
Expand Down Expand Up @@ -937,9 +954,9 @@
return;
}

var hour = this.hour,
minute = this.minute.toString().length === 1 ? '0' + this.minute : this.minute,
second = this.second.toString().length === 1 ? '0' + this.second : this.second;
var hour = this.paddedTime(this.hour),
minute = this.paddedTime(this.minute, true),
second = this.paddedTime(this.second, true);

if (this.showInputs) {
this.$widget.find('input.bootstrap-timepicker-hour').val(hour);
Expand Down
12 changes: 10 additions & 2 deletions spec/js/TimepickerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ describe('Timepicker feature', function() {
expect(tp2.$widget.find('.bootstrap-timepicker-meridian').val()).toBe('AM');
});

it('should ensure that 24 hour widgets pad the hours on display', function() {
tp3.setTime('2:04:05');
expect($input3.val()).toBe('02:04:05');
expect(tp3.$widget.find('.bootstrap-timepicker-hour').val()).toBe('02');
expect(tp3.$widget.find('.bootstrap-timepicker-minute').val()).toBe('04');
expect(tp3.$widget.find('.bootstrap-timepicker-second').val()).toBe('05');
});

it('should be able get & set the pickers time', function() {
tp1.setTime('11:15 PM');
expect(tp1.getTime()).toBe('11:15 PM');
Expand All @@ -180,7 +188,7 @@ describe('Timepicker feature', function() {
tp1.setTime('1');
expect(tp1.getTime()).toBe('1:00 AM');
tp3.setTime('1');
expect(tp3.getTime()).toBe('1:00:00');
expect(tp3.getTime()).toBe('01:00:00');

tp1.setTime('13');
expect(tp1.getTime()).toBe('12:00 AM');
Expand Down Expand Up @@ -218,7 +226,7 @@ describe('Timepicker feature', function() {
expect(tp3.getTime()).toBe('23:20:00', 'setTime with 2320 on tp3');

tp3.setTime('0:00');
expect(tp3.getTime()).toBe('0:00:00', 'setTime with 0:00 on tp3');
expect(tp3.getTime()).toBe('00:00:00', 'setTime with 0:00 on tp3');
});

it('should update picker on blur', function() {
Expand Down