JavaScript calculates the day difference of of between two dates


One: calculate the number of days between two dates

For example:     Str1   =   “2002-01-20”     Str2   =   “2002-10-11”   How do you calculate the number of days between str1 and str2 using javaScript?  

<html
<head
<meta  http-equiv="Content-Type"  content="text/html;  charset=gb2312"
<title></title
</head

<body
<button  onClick="btnCount_Click()"> Counting days off </button
<script  language="JavaScript"

   function  btnCount_Click(){ 
       s1  =  "2002-1-10" 
       s2  =  "2002-10-1" 
       alert(DateDiff(s1,s2)) 
   } 

   //A function of the number of days, universal & PI;
   function  DateDiff(sDate1,  sDate2){    //SDate1 and sDate2 are in 2002-12-18 format & NBSP;
       var  aDate,  oDate1,  oDate2,  iDays 
       aDate  =  sDate1.split("-"
       oDate1  =  new  Date(aDate[1+  '-'  +  aDate[2+  '-'  +  aDate[0])    //Convert to 12-18-2002 & NBSP;
       aDate  =  sDate2.split("-"
       oDate2  =  new  Date(aDate[1+  '-'  +  aDate[2+  '-'  +  aDate[0]) 
       iDays  =  parseInt(Math.abs(oDate1  -  oDate2)  /  1000  /  60  /  60  /24)    //Converts the number of milliseconds off into the number of days & NBSP;
       return  iDays 
}

Two: calculate the date after a certain number of days

In JavaScript, you calculate what the days after the date of the day are. Far from being as convenient as it is in.net, a function can solve the problem. On this problem, I puzzled for a period of time, and finally through the introduction of a net friend to solve the problem. Post it and share it.

<script  language="javascript"  type="text/javascript"
var  startDate  =  new  Date  ();           var  intValue  =  0; 
var  endDate  =  null; 

intValue  =  startDate.getTime();            intValue  +=  100  *  (24  *  3600  *  1000); 
endDate  =  new  Date  (intValue); 
alert  (endDate.getFullYear()+"-"+ (endDate.getMonth()+1)+"-"+ endDate.getDate()); 
</script

The 100 above represents the date after 100 days, which you can change. Date.getTime() in JS, can only be 1970.01.01 after the Date; There are months are 0-11, a little different, avoid oh. Of course, you can also calculate the date after a certain date.

<script  language="javascript"  type="text/javascript"
var  startDate  =  new  Date  (2007,  (8-1),  1,  10,  10,  10);
var  intValue  =  0; 
var  endDate  =  null; 

intValue  =  startDate.getTime();            intValue  +=  100  *  (24  *  3600  *  1000);
endDate  =  new  Date  (intValue); 
alert  (endDate.getFullYear()+"-"+ (endDate.getMonth()+1)+"-"+ endDate.getDate()); 
</script