JavaScript calculates the difference in days between two dates

Source: Internet
Author: User

  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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.