Calculating the difference between two dates

Solution 1:


<script language="JavaScript">
<!--
function dstrToUTC(ds) {
 var dsarr = ds.split("/");
 var mm = parseInt(dsarr[0],10);
 var dd = parseInt(dsarr[1],10);
 var yy = parseInt(dsarr[2],10);
 return Date.UTC(yy,mm-1,dd,0,0,0);
}

function datediff(ds1,ds2) {
 var d1 = dstrToUTC(ds1);
 var d2 = dstrToUTC(ds2);
 var oneday = 86400000;
 return (d2-d1) / oneday;
}

// test cases are below

var a; var b;

a = "01/09/1999";
b = "01/10/1999";
document.write("From "+a+" to "+b+" is "+datediff(a,b)+" day(s)<br>");

a = "01/12/1999";
b = "01/19/1999";
document.write("From "+a+" to "+b+" is "+datediff(a,b)+" day(s)<br>");

a = "01/19/1999";
b = "01/12/1999";
document.write("From "+a+" to "+b+" is "+datediff(a,b)+" day(s)<br>");

a = "01/03/1999";
b = "01/13/1999";
document.write("From "+a+" to "+b+" is "+datediff(a,b)+" day(s)<br>");

a = "04/30/1999";
b = "05/01/1999";
document.write("From "+a+" to "+b+" is "+datediff(a,b)+" day(s)<br>");

a = "05/30/1999";
b = "06/01/1999";
document.write("From "+a+" to "+b+" is "+datediff(a,b)+" day(s)<br>");

a = "02/28/1999";
b = "03/01/1999";
document.write("From "+a+" to "+b+" is "+datediff(a,b)+" day(s)<br>");

a = "02/28/2000";
b = "03/01/2000";
document.write("From "+a+" to "+b+" is "+datediff(a,b)+" day(s)<br>");

a = "01/01/1999";
b = "12/31/1999";
document.write("From "+a+" to "+b+" is "+datediff(a,b)+" day(s)<br>");

a = "01/01/2000";
b = "12/31/2000";
document.write("From "+a+" to "+b+" is "+datediff(a,b)+" day(s)<br>");

a = "12/15/1999";
b = "01/15/2001";
document.write("From "+a+" to "+b+" is "+datediff(a,b)+" day(s)<br>");

// -->
</script>

Solution 2:

date2 = 02/09/2009 is not considered to be a date. it works this way. First it devides 02/09, it returns 0.2222222222222222 and its been divided by 2009 (0.2222222222222222/2009). finally you got an result date2 = 0.0001106133510314695. same way it calculates the result for date1.

this is not a valid operation. If you wanted to define the date. make sure that, you placed the data in a right date format.

use either new Date() or Date.parse("02/09/2009")

Edit:

   new Date(Date.parse("03/12/2009")-Date.parse("02/09/2009")).toLocaleDateString() Or
   new Date(date1- date2).toLocaleDateString()

isnt that work..??

Edit :

may be this will work.. can u try this..

 Date.parse("03/12/2009")-Date.parse("02/09/2009") / (24*60*60*1000) 

and it returns 31 days

it seems working for me.. but in my time zone it took 03/12/2009 as 3rd month 11th day and year 2009

(24*60*60*1000) = Number of milliseconds per day

Solution 3:

getDate is a method of Date object. as any docs clearly state it returns the day of the month in range 0 to 31. it wouldn't make sense to try to subtract one from the other if it's not the same month.

Solution 4:

diff.setTime(Math.abs(date1.getTime() - date2.getTime()));

timediff = diff.getTime();

weeks = Math.floor(timediff / (1000 * 60 * 60 * 24 * 7));
timediff -= weeks * (1000 * 60 * 60 * 24 * 7);

days = Math.floor(timediff / (1000 * 60 * 60 * 24)); 
timediff -= days * (1000 * 60 * 60 * 24);

hours = Math.floor(timediff / (1000 * 60 * 60)); 
timediff -= hours * (1000 * 60 * 60);

mins = Math.floor(timediff / (1000 * 60)); 
timediff -= mins * (1000 * 60);

secs = Math.floor(timediff / 1000); 
timediff -= secs * 1000;

alert(weeks + " weeks, " + days + " days, " + hours + " hours, " + mins + " minutes, and " + secs + " seconds");

Solution 5:

use Date.parse(date1) - Date.parse(date2)