Calculate minutes difference between two times in JavaScript

SajidHussa
 
on Sep 17, 2021 06:37 AM
441 Views

i have two textbox and my time format is 24 hrs

textbox1 value is 12:00

textbox2 value is 13:30

i want to calculate minute between this two time and also check minute is not negative on button click in jquery or javascript 

the result is 90Min 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Sep 18, 2021 12:11 AM

Hi SajidHussa,

Refer below code.

HTML

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <input type="text" id="textbox1" value="12:00" /><br />
    <input type="text" id="textbox2" value="13:30" /><br />
    <input type="button" value="Calculate" onclick="OnCalculate()" />
    <script type="text/javascript">
        function OnCalculate() {
            var today = new Date().toJSON().slice(0, 10).replace(/-/g, '/');
            var startTime = new Date(today + " " + document.getElementById('textbox1').value);
            var endTime = new Date(today + " " + document.getElementById('textbox2').value);
            var difference = endTime.getTime() - startTime.getTime();
            alert(Math.round(difference / 60000) + " Minutes");
        }
    </script>
</body>
</html>

Demo