In this article, we will explore the process of determining the interval between two dates using JavaScript. When the appropriate methods are employed, calculating the difference becomes a simple task.
To compute the difference between dates in JavaScript, the date object is essential. This date object in JavaScript allows you to retrieve the year, month, and day. You can determine the difference between two dates in terms of days, years, or even milliseconds.
Next, we will examine examples that demonstrate how to compute the difference between two dates in terms of days, years, or milliseconds.
In the initial illustration, we will explore how to determine the difference in days between two dates by utilizing JavaScript.
Example1
This section demonstrates how to compute the difference in the number of days between two given dates. In this illustration, we employ a method to determine the day count difference.
In this section, we begin by establishing two dates utilizing the new date function. Following this, we determine the time interval between the two designated dates through the built-in getTime method. We then compute the total number of days by dividing the time difference between the two dates by the number of milliseconds that comprise a single day, which is calculated as (10006060*24).
In this context, the variable d1 is designated to hold the initial date, while the variable d2 is assigned to contain the subsequent date. The variable diff is utilized to record the time difference, and the variable daydiff is employed to capture the difference in dates.
<html>
<head>
</head>
<body>
<h1> Hello World :) :) </h1>
<p> This is an example of getting the difference between two dates using JavaScript. </p>
<script>
var d1 = new Date("08/14/2020");
var d2 = new Date("09/14/2020");
var diff = d2.getTime() - d1.getTime();
var daydiff = diff / (1000 * 60 * 60 * 24);
document.write(" Total number of days between <b> " + d1 + " </b> and <b> " + d2 + " </b> is: <b> " + daydiff + " days </b>" );
</script>
</body>
</html>
Output
Example2
This is an illustration of determining the interval between two dates expressed in years. In this instance, the calculation is focused on how many years have elapsed from "10/02/1869" to today's date.
In this context, the variable d1 is assigned the initial date, while the variable d2 holds the current date. The variable diff calculates the time difference between these two dates, and the variable daydiff represents the difference in days between them. By utilizing this method, we can also determine an individual's age.
<html>
<head>
</head>
<body>
<h1> Hello World :) :) </h1>
<p> This is an example of getting the difference between two dates using JavaScript. </p>
<script>
var d1 = new Date("10/02/1969");
var d2 = new Date();
var diff = d2.getTime() - d1.getTime();
var daydiff = (diff / 31536000000).toFixed(0);
document.write(" Total numbers of years since <b> 2nd October 1969 </b> is: <b> " + daydiff + " years </b>" );
</script>
</body>
</html>
Output