In this article, we will learn how to find or calculate the total number of days between two dates in javascript. For calculating the total number of days between two dates, use the in-built javascript getTime() function and get the internal millisecond value of the date using the getTime() function.
For Calculating the total number of days between two dates, follow these steps:
// To set two dates to two variables
var Date1 = new Date("01/01/2020");
var Date2 = new Date("02/01/2020");
Find the time difference between two dates by Subtracting dates(Date2.getTime() - Date1.getTime()).
// To calculate the time difference between two dates
var timeDifference = Date2.getTime() - Date1.getTime();
Step 3: Divide the time difference of both the dates by the total number of milliseconds in a day(1000*60*60*24), and get the total number of days between two dates.
// To calculate the Total number of days between two dates
var DaysDifference = Difference_In_Time / (1000 * 3600 * 24);
Final Step: Display the Result by using document.Write().
Here is the complete code to get the total no. of days between two dates in javascript.
// To set two dates to two variables
var Date1 = new Date("01/01/2020");
var Date2 = new Date("02/01/2020");
// To calculate the time difference between two dates
var timeDifference = Date2.getTime() - Date1.getTime();
// To calculate the Total number of days between two dates
var DaysDifference = timeDifference / (1000 * 3600 * 24);
//To display the Total number of days (output)
document.write("Total number of days between given two dates<br />"
+ Date1 + "<br> and <br>"
+ Date2 + " is: <br> "
+ DaysDifference );
Total number of days between given two dates
Wed Jan 01 2020 00:00:00 GMT+0530 (India Standard Time)
and
Sat Feb 01 2020 00:00:00 GMT+0530 (India Standard Time) is:
31
I hope this article will help you to understand how to get or calculate or get the total number of days between two dates in javascript
Share your valuable feedback, please post your comment at the bottom of this article. Thank you!
Comments