JQuery UI Slider for time

Nisanth Kumar picture Nisanth Kumar · Feb 17, 2010 · Viewed 44k times · Source

Hi I need to implement a slider for 24 hour time range . I like to use jquery ui slider for this . I have written below code

<script type="text/javascript">
$(function() {
    $(".slider-range").slider({
        range: true,
        min: 0,
        max: 23.59,
        step: 0.15
    });
});
</script>

I like the range is like 01:00----01:59

How i gave the colon(:) instead of dot(.) . Also the range waas gone beyond 59 like 05:85 . Please help me to create a time slider

Answer

Tatu Ulmanen picture Tatu Ulmanen · Feb 17, 2010

Do not use hours as a unit, use minutes instead. Then apply a slide event that converts the minutes to hours:

$(function() {
    $(".slider-range").slider({
        range: true,
        min: 0,
        max: 1440,
        step: 15,
        slide: function(e, ui) {
            var hours = Math.floor(ui.value / 60);
            var minutes = ui.value - (hours * 60);

            if(hours.toString().length == 1) hours = '0' + hours;
            if(minutes.toString().length == 1) minutes = '0' + minutes;

            $('#something').html(hours+':'+minutes);
        }
    });
});