Calculates the number of days between two dates JS code

Source: Internet
Author: User
Tags date1 getdate

One: Calculate the number of days difference between two dates

The code is as follows Copy Code


var s = (new Date (ParamObj.end_date.replace (/-/g, "/"))-(new Date (ParamObj.start_date.replace (/-/g, "/")));
var day = s/1000/60/60/24


Example 2

JS can actually directly date1-date2, but also converted to millisecond calculation time difference.

  code is as follows copy code


//Two date Interval days  
function DateDiff (sDate1, SDate2) {//sdate1 and sDate2 are 2002-12-18 format   
var adate, ODate1, ODate2, idays  
Adate = Sdate1.split ("-")   
ODate1 = new Date (adate[1] + '-' + adate[2] + '- ' + adate[0])//Convert to 12-18-2002 format   
Adate = Sdate2.split ("-")   
ODate2 = new Date (adate[1] + ' -' + adate[2] + '-' + adate[0] '   
Idays = parseint (Math.Abs (odate1-odate2)/1000/60/60/24)//Put the difference in milliseconds Convert number to days   
return idays  
}

Second: Calculate the date after a certain number of days

In JavaScript, count the days after the day's date. Far from being in. NET, a function can solve the problem. On this issue, I was troubled for some time, finally through a Netizen's introduction to solve the problem. Put it together and share it.

The code is as follows Copy Code


<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 above 100 represents 100 days after the date that you can modify. JS in Date.gettime (), only after the date of 1970.01.01; there are 0-11 months, a little different, avoid oh. Of course, you can also calculate the date after a specific date.

The code is as follows Copy Code


<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>

If none of the above is calculated for a leap year, the following example is finished

The code is as follows Copy Code


Judge whether it is a leap year
function Isleapyear (year) {
if (year% 4 = 0 && (year% 100!= 0) | | (Year% 400 = 0))
{
return true;
}
return false;
}
Two dates before and after judgment
function Validateperiod (fyear,fmonth,fday,byear,bmonth,bday) {
if (Fyear < byear) {
return true;
}else if (fyear = = byear) {
if (Fmonth < Bmonth) {
return true;
else if (Fmonth = = Bmonth) {
if (Fday <= bday) {
return true;
}else {
return false;
}
} else {
return false;
}
}else {
return false;
}
}
Calculate the difference for two dates
function DateDiff (D1,D2) {
var disnum=comparedate (D1,D2);
return disnum;
}
function Comparedate (DATE1,DATE2)
{
var regexp=/^ (d{1,4}) [-|.] {1} (d{1,2}) [-|.] {1} (d{1,2}) $/;
var monthdays=[0,3,0,1,0,1,0,0,1,0,0,1];
Regexp.test (date1);
var date1year=regexp.$1;
var date1month=regexp.$2;
var date1day=regexp.$3;
Regexp.test (DATE2);
var date2year=regexp.$1;
var date2month=regexp.$2;
var date2day=regexp.$3;
if (Validateperiod (Date1year,date1month,date1day,date2year,date2month,date2day)) {
Firstdate=new Date (Date1year,date1month,date1day);
Seconddate=new Date (Date2year,date2month,date2day);
Result=math.floor ((Seconddate.gettime ()-firstdate.gettime ())/(1000*3600*24));
for (j=date1year;j<=date2year;j++) {
if (Isleapyear (j)) {
monthdays[1]=2;
}else{
monthdays[1]=3;
}
for (i=date1month-1;i<date2month;i++) {
Result=result-monthdays[i];
}
}
return result;
}else{
Alert (' Sorry the first time must be less than the second time, thank you! ');
Exit
}
}

Call this function to pass two time values: 2013-01-19 2013-12-19

Days = DateDiff (D1,D2);

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.