- 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().
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
- 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.
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
- Program to find the number of days between two dates in PHP
- How to calculate the difference between two dates in PHP?
- Return all dates between two dates in an array in PHP
- How to calculate the number of words in a string using JQuery?
- Comparing two dates in PHP
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.