Calculate days between two dates in jquery.


Using moment.js, We can calculate days between two dates in jquery.
JavaScript

//Dates in string format
var strStartDate = "07/29/2017"; //MM/DD/YYYY
var strEndDate = "08/30/2017"; //MM/DD/YYYY

//Convert string dates to Date format
var StartDate = moment(strStartDate,"MM/DD/YYYY");
var EndDate = moment(strEndDate,"MM/DD/YYYY");

//Calculate duration
var duration = moment.duration(EndDate.diff(StartDate));

//Convert duration in days
var days = duration.asDays();

//Display on screen
$("#StartDate").html(StartDate.format("MMM DD, YYYY"));
$("#EndDate").html(EndDate.format("MMM DD, YYYY"));
$("#Days").html(days);


Html

<h2>Calculate days between two dates</h2>
Start Date : <div id="StartDate"></div>
End Date : <div id="EndDate"></div>
Days : <div id="Days"></div>


Result
Calculate days between two dates
Start Date :
Jul 29, 2017
End Date :
Aug 30, 2017
Days :
32

Try it

Leave a Reply

Your email address will not be published. Required fields are marked *