[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>
    <title>Page Title</title>
    <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 =)");

Ashok Patel

I'm an electronic engineer working in a multinational company,having good experience on Electronics and electrical engineers design and oversee production of electronic equipment such as radios, televisions, computers, washing machines and telecommunication systems.I like to do RND and Research.I also have hands on experience graphic design software and in web designing having great command on ASP.NET, HTML5, JavaScript, T-SQL, JQuery.

Add comment

Donate for Corona Victim

Corona Virus Relief Fund

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