Reputation: 3359
I have jquery slider and I want to select time from to. But minutes from 0-9 showing as 13:7
but not 13:07
jQuery(function() {
jQuery('#slider-time').slider({
range: true,
min: 300,
max: 1280,
step: 1,
values: [ 300, 793 ],
slide: function( event, ui ) {
var hours1 = Math.floor(ui.values[0] / 60);
var minutes1 = ui.values[0] - (hours1 * 60);
if(hours1.length < 10) {hours1= '0' + hours};
if(minutes1.length < 10) {minutes1 = '0' + minutes};
if(minutes1 == 0) minutes1 = '00';
var hours2 = Math.floor(ui.values[1] / 60);
var minutes2 = ui.values[1] - (hours2 * 60);
if(hours2.length < 10) {hours2= '0' + hours};
if(minutes2.length < 10) {minutes2 = '0' + minutes};
if(minutes2 == 0) minutes2 = '00';
jQuery('#amount-stop').val(hours1+':'+minutes1 );
jQuery('#amount-start').val(hours2+':'+minutes2 );
}
});
});
Upvotes: 3
Views: 84
Reputation: 841
Including the suggestion in my comment ("Numbers don't have a length, use something like: if(minutes2 < 10)
"), also maybe make a function timeToString()
to reduce repeated code and enhance manageability, e.g.:
function timeToString(time){
var hours = Math.floor(time / 60),
minutes = time - (hours * 60);
if(hours < 10) {hours= '0' + hours};
if(minutes < 10) {minutes = '0' + minutes};
if(minutes == 0) minutes = '00';
return hours + ":" + minutes;
}
and use that within your block, e.g.:
jQuery('#amount-stop').val( timeToString(ui.values[0]) );
jQuery('#amount-start').val( timeToString(ui.values[1]) );
Upvotes: 1
Reputation: 101
Try using the following code to calculate hour and minute values
var hours1 = parseInt(ui.values[0] / 60 % 24, 10);
var minutes1 = parseInt(ui.values[0] % 60, 10);
In my case they return the values in two digits.
Upvotes: 1