[Solved]-Difference between two dates in minutes Javascript


If you are seeking a JavaScript function to calculate the difference between two dates in minutes, you have come to the right place. Here is a simple JavaScript code.

function GetdiffinMinute()
        {
            var d1 = new Date("12/04/2020")
            var d2 = new Date("11/04/2020")
            var diff = (d1.getTime() - d2.getTime()) / 60000;
            var mintdiff = Math.abs(Math.round(diff));
            return mintdiff
        }

Subtracting two Date objects gives you the result in milliseconds. If you want the result in minutes, divide the result by 1000 to get the number of seconds. Then, divide that value by 60 to get the number of minutes. So, you can say that dividing the value by 60000 gives you the difference in minutes.

Using the above concept, you can also write a generic JavaScript function which will return the difference between two dates in minutes, hours, days, and milliseconds."

<!DOCTYPE html>
<html>
<head>
    <title>Page Title</title>
</head>
<body>
    <button onclick="GetdiffinMinute()" type="button">Click</button>
    <script type="text/javascript">
        function GetdiffinMinute()
        {
            var todaydate = new Date();
            var Newyearday = new Date("2020-01-01");
            var diffMs = (todaydate - Newyearday); //diffrence in  milliseconds between now & Christmas
            var diffDays = Math.floor(diffMs / 86400000); //diffrence in  days
            var diffHrs = Math.floor((diffMs % 86400000) / 3600000); //diffrence in hours
            var diffMins = Math.round(((diffMs % 86400000) % 3600000) / 60000); //diffrence in minutes
            alert(diffDays + " days, " + diffHrs + " hours, " + diffMins + " minutes until Christmas 2009 =)");
        }
    </script>
</body>
</html>


  • It creates a new Date object representing the current date and time (todaydate).
  • It creates another Date object representing January 1, 2020 (Newyearday).
  • It calculates the difference in milliseconds between the current date and January 1, 2020, and stores it in the diffMs variable.
  • It calculates the number of days remaining by dividing the milliseconds difference by the number of milliseconds in a day (86400000) and rounding down (Math.floor(diffMs / 86400000)).
  • It calculates the number of hours remaining by finding the remainder of dividing the milliseconds difference by the number of milliseconds in a day, then dividing that by the number of milliseconds in an hour (3600000) and rounding down (Math.floor((diffMs % 86400000) / 3600000)).
  • It calculates the number of minutes remaining similarly by finding the remainder of dividing the milliseconds difference by the number of milliseconds in a day, then the remainder of that by the number of milliseconds in an hour, and finally dividing that by the number of milliseconds in a minute (60000) and rounding (Math.round(((diffMs % 86400000) % 3600000) / 60000)).
  • Finally, an alert dialog displays the calculated days, hours, and minutes until Christmas 2009.