Click here to Skip to main content
15,885,954 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone ,

Please help me to find out days difference between two date.
I have a following data

var startDate = $("#ctl00_cntPlcHld_txtFrom").val();
var startDate = $("#ctl00_cntPlcHld_txtTo").val();


i got following data from this


09-12-2014 (dd-mm-yyyy)
09-01-2014 (dd-mm-yyyy)

I want difference between above two date.

like in above example ans would be 11


Thanks in advance....
Posted

JavaScript
function days_between(date1, date2) {

    // The number of milliseconds in one day
    var ONE_DAY = 1000 * 60 * 60 * 24

    // Convert both dates to milliseconds
    var date1_ms = date1.getTime()
    var date2_ms = date2.getTime()

    // Calculate the difference in milliseconds
    var difference_ms = Math.abs(date1_ms - date2_ms)

    // Convert back to days and return
    return Math.round(difference_ms/ONE_DAY)

}


If you having any formatting issue you can follow this way with your date format.

JavaScript
var date1 = new Date("7/11/2010");
var date2 = new Date("12/12/2010");
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 


Js fiddle example here, go to the page and click on run.

http://jsfiddle.net/JS69L/323/[^]
 
Share this answer
 
v2
You can try this way

text/javascript
var startDate ="09-01-2014"; //$("#ctl00_cntPlcHld_txtFrom").val();
var endDate = "09-12-2014"; //$("#ctl00_cntPlcHld_txtTo").val();

var startDateFormated = startDate.split("-")[2] +'-'+startDate.split("-")[0]+'-'+startDate.split("-")[1];
var endDateFormated=endDate.split("-")[2] +'-'+endDate.split("-")[0]+'-'+endDate.split("-")[1];

 alert(startDateFormated);
   start = new Date(startDateFormated),
    end   = new Date(endDateFormated),
    diff  = new Date(end - start),
    days  = diff/1000/60/60/24;
alert(days)
<pre />
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900