*50*

# 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:

- Define two date variables in JavaScript
- Initialize them by creating the date objects using
**new Date()** - After defining the dates, calculate the time difference between them by subtracting one date from another date using
**date2.getTime() – date1.getTime();** - 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
- 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*

**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 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*

**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.

### 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:

- Define two date variables, i.e., date1 and date2
- Initialize the date1 variables by creating the date objects using
**new Date()**, which will take system date by default. - Initialize the date2 variables by creating the date objects using
**new Date()**and provide a date in it. - 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;** - 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 - 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*

**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**

### 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:

- Define the current date (system date) by creating a date object using the
**new Date()**. - 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). - 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.
- 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. - 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*

**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**

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.