# How to calculate the number of days between two dates in javascript?

Calculating the number of days between two dates in JavaScript required to use date object for any kind of calculation. For that, first, get the internal millisecond value of the date using the in-built JavaScript getTime() function. As soon as both the dates get converted, proceed further by subtracting the later one from the earlier one which in turn returns the difference in milliseconds. Later, the final result can be calculated by dividing the difference (which is in milliseconds) of both the dates by the number of milliseconds in one day.

Syntax:

`Date.getTime()`

Approach 1:

• Define two dates using new Date().
• Calculate the time difference of two dates using date2.getTime() – date1.getTime();
• Calculate the no. of days between two dates, divide the time difference of both the dates by no. of milliseconds in a day (1000*60*60*24)
• Print the final result using document.write().

Example 1: The following JavaScript program will illustrate the solution

 ` `

Output:

```Total number of days between dates
Sun Jun 30 2019 00:00:00 GMT-0700 (Pacific Daylight Time)
and
Tue Jul 30 2019 00:00:00 GMT-0700 (Pacific Daylight Time) is:
30
```

Approach 2:

• Defined the present date by using the new date() which will give the present date and the Christmas date by date.getFullYear() (this will get the year, 0-11 are the months in JavaScript).
• if condition in order to calculate the total number of days if the Christmas has been passed already (this will calculate the no. of days between the present date and the Christmas of the next year).
• Use Math.round(christmas() – present_date.getTime()) Divided one day’s miliseconds to Calculate the result in milliseconds and then converting into days

Example 2: This example we calculated the number of days until Christmas day.

 ` `

Output:

```Number of days remaining till christmas
Sun Jun 30 2019 11:33:51 GMT-0700 (Pacific Daylight Time)
and
Wed Dec 25 2019 00:00:00 GMT-0800 (Pacific Standard Time) is:
178
```

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.