<!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>
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.
Add comment