Home » Calculate days between two dates in JavaScript

Calculate days between two dates in JavaScript

by Online Tutorials Library

Calculate days between two dates in JavaScript

Sometimes we need to calculate the number of days between two dates. This can be done using the JavaScript programming language. JavaScript provides a math function Math.floor() method to calculate the days between two dates. In JavaScript, we have to use the date object for any calculation to define the date using new Date().

To learn more about JavaScript in detail step by step follow our JavaScript tutorial.

We will calculate the number of days in two ways:

  • Calculate days by entering two dates
  • Calculate days from current dates

We will discuss both the methods in detail with examples. Along with that, we will calculate the days remaining in Christmas day from today (date will be taken from the system).

  • Find the number of days remaining in Christmas from the current date

Approach 1: Calculate the days by entering two dates

This approach will help to calculate the days between two dates by providing two different dates in code. Follow the below step:

  1. Define two date variables in JavaScript
  2. Initialize them by creating the date objects using new Date()
  3. After defining the dates, calculate the time difference between them by subtracting one date from another date using date2.getTime() – date1.getTime();
  4. Now next step is to calculate the days between the dates. So, we will divide the time difference of both dates by the milliseconds in a day, i.e., 1000*60*60*24
  5. At last, print the result calculated using the document.write() method.

Now, we will convert these steps into actual implementation. See the code below:

Copy Code

Test it Now

Output

Number of days between dates   Mon Jul 20 2015 00:00:00 GMT+0530 (India Standard Time) and   Tue Sep 20 2016 00:00:00 GMT+0530 (India Standard Time) are:   502 days  

Screenshot

Calculate of days between two dates in JavaScript

Calculate days using dynamic HTML form

It is a dynamic JavaScript example in which a user can provide the input by itself on web instead of providing the dates input in code by the programmer. The user will dynamically choose the dates from the calendar and enter the two dates in input field and calculate the number of days between two dates by clicking on a button. See the code below:

Copy Code

Test it Now

Output

Look at the screenshot below having two calendar input fields, one submit button to perform all the calculation and one more field to display calculated result.

Calculate of days between two dates in JavaScript

Approach 2: Calculate the days from current date

In this approach, we will use the math function Math.floor() to calculate the days. Follow the below step:

  1. Define two date variables, i.e., date1 and date2
  2. Initialize the date1 variables by creating the date objects using new Date(), which will take system date by default.
  3. Initialize the date2 variables by creating the date objects using new Date() and provide a date in it.
  4. Now use the Math.abs() method to calculate the total seconds between two dates. So, divide the difference of dates by the milliseconds in one second Math.abs(date2-date1) / 1000;
  5. Now next step is to calculate the number of days using the Math.floor() method. So, divide the calculated result from previous steps (total seconds between two dates) by 60*60*24
  6. At last, print the result calculated using the document.write() method.

Now, we will convert these steps into actual implementation. See the code below:

Copy Code

Test it Now

Output

Number of days between dates   Sun Sep 27 2020 19:42:13 GMT+0530 (India Standard Time) and   Tue Dec 15 2021 21:45:10 GMT+0530 (India Standard Time) are:   444 days   

Screenshot

Calculate of days between two dates in JavaScript

Example 3: Calculate the days remaining in Christmas day from the current date

We have one more example in which we will calculate the days till Christmas Day means for a specific date. Follow the below steps:

  1. Define the current date (system date) by creating a date object using the new Date().
  2. Define one more date using new Date() class method and fetch the Christmas date by date.getFullYear() method in it. The date.getFullYear() method will return the year (0-11 months in JavaScript).
  3. In case Christmas has been already passed away this year, it will return the number of days remaining in Christmas of next year from the current date.
  4. Now, its time to use Math.round() function to calculate the result in milliseconds and then convert this result into days. So, divide the Math.round(Christmas() – present_date.getTime()) by the total milliseconds in a day, i.e., 1000*60*60*24.
  5. Remove the decimal value from the result and display it to the web using document.write() method.

Now, convert these steps into actual implement to calculate the days remaining in Christmas. The implementation is as easy as the steps are. See the below code:

Copy Code

Test it Now

Output

Number of days remaining in Christmas day:   Mon Sep 28 2020 14:32:50 GMT+0530 (India Standard Time) and   Fri Dec 25 2021 00:00:00 GMT+0530 (India Standard Time) are:   87 days  

Screenshot

Calculate of days between two dates in JavaScript

So, here was all the necessary examples (static and dynamic both) in JavaScript for calculating the days difference. To learn more about JavaScript in detail step by step follow our JavaScript tutorial.


You may also like