[Solved]-Difference between two dates in minutes Javascript

[Solved]-Difference between two dates in minutes Javascript

If you are looking for a JavaScript function to get the difference between the two dates in minutes then you have come to the right place.Here is the 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 .Now we want result in minutes  so that Dividing the result by 1000 gives you the number of seconds. And then Dividing that value by 60 gives you the number of minutes. So you can say that divide the value by 60000 gives you difference in minutes.

Using Above Concept you can also write a generic javascript function which will return the difference between two dates in minute,hours ,days,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>

Ashok Patel

I'm a software engineer, having good experience in software programming web designing with great command on ASP.NET, React JS, Angular JS,.NET Core HTML5, JavaScript, T-SQL, JQuery.
Also have great experience in Electronics and electrical engineers design.
I like to do RND and Research.

Add comment

Your Header Sidebar area is currently empty. Hurry up and add some widgets.