[Solved]-How to get the number of days between two dates in JavaScript?

[Solved]-How to get the number of days between two dates in JavaScript?

Recently I’m working on a project in which I need to get the number of days between two dates. So I thought I should share sample code for other developers.
Below is a quick and simple code for calculating the number of days between two dates in JavaScript. It based on the concept that you can get the milliseconds between two dates by subtracting two date objects and then convert this result into number of days.
 
<!DOCTYPE html>
<html>
<head>
    <title>DotNetPeTips-JavaScript</title>
</head>
<body>
    <script>
        var second, first;
        first = new Date("2020-05-02");
        second = new Date("2020-04-02");
        var days = Math.round((first - second) / (1000 * 60 * 60 * 24));
        document.write("<br>Difference: " + days);
    </script>
</body>
</html>

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.