Quantcast
Channel: How do I get the difference between two Dates in JavaScript? - Stack Overflow
Viewing all articles
Browse latest Browse all 20

Answer by Joel Coehoorn for How do I get the difference between two Dates in JavaScript?

$
0
0

If you don't care about the time component, you can use .getDate() and .setDate() to just set the date part.

So to set your end date to 2 weeks after your start date, do something like this:

function GetEndDate(startDate){    var endDate = new Date(startDate.getTime());    endDate.setDate(endDate.getDate()+14);    return endDate;}

To return the difference (in days) between two dates, do this:

function GetDateDiff(startDate, endDate){    return endDate.getDate() - startDate.getDate();}

Finally, let's modify the first function so it can take the value returned by 2nd as a parameter:

function GetEndDate(startDate, days){    var endDate = new Date(startDate.getTime());    endDate.setDate(endDate.getDate() + days);    return endDate;}

Viewing all articles
Browse latest Browse all 20

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>