Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: change Minutes into hour

Tags:

jquery

time

input

I am using jQuery to convert minutes into hour, whenever minutes exceeds more than 59; hour is changed to 1.

I have tried this so far-

<script type="text/javascript">
    $(document).ready(function () {
        $('.minutes').change(function () {
            if ($('.minutes').val() > 59) {
                Math.floor($('.hour').val()) = ($(this).val()) + 1;
            }

        });
    });


</script>

Jsfiddle link- Minutes into hour

I want minutes to show converted hour every time it exceeds multiplications of 60.

Thank you for your time.

like image 948
Manoz Avatar asked Dec 05 '25 10:12

Manoz


2 Answers

here's the JSFiddle

$(document).ready(function () {
    $('.minutes').change(function () {
        if ($('.minutes').val() > 59) {
            //Math.floor($('.hour').val() )= $(this).val() + 1;
            var min = $('.minutes').val();
            var hr = Math.floor(min / 60);
            var hrTemp = Math.floor($('.hour').val());
            var remMin = min % 60;
            $('.hour').val(hr + hrTemp);
            $('.minutes').val(remMin);
        }

    });
});
like image 200
Vond Ritz Avatar answered Dec 07 '25 20:12

Vond Ritz


In cased that minutes is more than 120, adding 1 doesn't produce the correct hour, instead of hard-coding the value by adding 1, you can divide the minutes by 60 and add the result to the .hour element's value.

$(document).ready(function () {
    $('.minutes').change(function () {
        var min = + this.value;
        if (min > 59) {
            this.value = min % 60;
            $('.hour').val(function (_, oldValue) {
                return +oldValue + Math.floor(min / 60);
            })
        }
    });
});

http://jsfiddle.net/GZEaH/

like image 37
undefined Avatar answered Dec 07 '25 19:12

undefined



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!