How can I calculate the difference between two times that are in 24 hour format?

后端 未结 5 1890
死守一世寂寞
死守一世寂寞 2020-12-01 18:23

In JavaScript, how can I calculate the difference between two times that are in 24 hour format?

Example: Get how many hours elapsed from 08:00:00 to

5条回答
  •  暖寄归人
    2020-12-01 18:57

    You dont need to convert them into dates for this specific case. Just Do this

    $(document).ready(function() {    
        function calculateTime() { 
            var hourDiff = 
            parseInt($("select[name='timestart']").val().split(':')[0],10) -         
            parseInt($("select[name='timestop']").val().split(':')[0],10);
    
            $("p").html("Hour Difference: " + hourDiff )         
        }
        $("select").change(calculateTime);
        calculateTime();
    });
    

    Working fiddle​

提交回复
热议问题