- 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 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 Christmas has 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 milliseconds to Calculate the result in milliseconds and then convert it into days
Example 2: In 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