One: Calculates the number of days between two dates
123<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">4<title></title>56 7<body>8<button onclick= "Btncount_click ()" > Calculate the difference between days </button>9<script language= "JavaScript" >Ten One functionBtncount_click () { AS1 = "2002-1-10" -S2 = "2002-10-1" - alert (DateDiff (S1,S2)) the } - - //A function that calculates the difference in days, general - functionDateDiff (sDate1, SDate2) {//sDate1 and SDate2 are 2002-12-18 formats + varadate, ODate1, ODate2, Idays -Adate = Sdate1.split ("-") +ODate1 =NewDate (adate[1] + '-' + adate[2] + '-' + adate[0])//Convert to 12-18-2002 format AAdate = Sdate2.split ("-") atODate2 =NewDate (adate[1] + '-' + adate[2] + '-' + adate[0]) -Idays = parseint (Math.Abs (odate1-odate2)/1000/60/60/24)//Convert the difference in milliseconds to a number of days - returnidays -}
Second: Calculate the date after a certain number of days
1<script language= "javascript" type= "Text/javascript" >2 varStartDate =NewDate (); 3 varIntvalue = 0; 4 varEndDate =NULL; 5 6Intvalue =Startdate.gettime (); I7Ntvalue + = 100 * (24 * 3600 * 1000); 8EndDate =NewDate (intvalue); 9Alert (enddate.getfullyear () + "-" + (Enddate.getmonth () +1) + "-" +enddate.getdate ()); Ten</script>
The above 100 represents 100 days after the date that you can modify. JS in Date.gettime (), only 1970.01.01 after the date; and the month is 0-11, a little different, avoid oh. Of course you can also calculate dates after a specific date.
1<script language= "javascript" type= "Text/javascript" >2 varStartDate =NewDate (2007, (8-1), 1, 10, 10, 10);3 varIntvalue = 0; 4 varEndDate =NULL; 5 6Intvalue =Startdate.gettime (); 7Intvalue + = 100 * (24 * 3600 * 1000);8EndDate =NewDate (intvalue); 9Alert (enddate.getfullyear () + "-" + (Enddate.getmonth () +1) + "-" +enddate.getdate ()); Ten</script>
JavaScript calculates the difference in days between two dates