JS calculate the past and future time distance now how many days?

Source: Internet
Author: User
Tags set time

Computes the incoming arbitrary time. Calculate how many days this time distance is now! Or calculate the past time distance is now over how many days!

There are two types of return values!

1. Negative values represent the past few days

2, positive value represents the distance set time and how many days

Note: The distance is not set by the day does not include (set that day), if necessary, the results of the calculation plus 1 can be!

/*format getbeforedate (' 2015,5,20 ')*//** Auth:[email protected]* time:2015.5.6 9:45* *******************/functiongetbeforedate (n) {varnow =NewDate (); varAftertime =NewDate (n); varYear =now.getfullyear (); varmon= Now.getmonth () +1; varday=now.getdate (); varYear_after =aftertime.getfullyear (); varMon_after = Aftertime.getmonth () +1; varDay_after =aftertime.getdate (); varCHS = 0; //get the number of days in the month    functionDaynumofmonth (year,month) {return32-NewDate (year,month-1,32). GetDate (); }    if(Aftertime.gettime ()-now.gettime () < 0){        varTemp1 =Day_after; varTemp2 =Mon_after; varTemp3 =Year_after; Day_after=Day ; Mon_after=Mon; Year_after=Year ; Day=Temp1; Mon=Temp2; year=Temp3; }    if(year = = Year_after) {//not across the year        if(Mon = = mon_after) {//Do not cross- year not cross-monthCHS + = day_after-Day ; }Else{//No cross-year cross-monthCHS + = Daynumofmonth (Year,mon)-day+1;//plus the first disgruntled             for(vari=1;i< mon_after-mon;i++) {CHS+ = Daynumofmonth (year,mon+i); } CHS+ = Day_after-1;//Plus        }        }Else{//There are cross-yearCHS + = Daynumofmonth (Year,mon)-day+1;//plus a month of dissatisfaction with the start year         for(varm=1;m<12-mon;m++) {CHS+ = Daynumofmonth (year,mon+m); }         for(varJ=1;j < year_after-year;j++){            if((year+j)%400 = = 0 | | (year+j)%4 = = 0 && (year+j)%100! = 0) {CHS+ = 366; }Else{CHS+ = 365; }        }         for(varN=1;n <= mon_after;n++) {CHS+=Daynumofmonth (year_after,n); } CHS+ = Day_after-1; }    if(Aftertime.gettime ()-now.gettime () < 0){        return-CHS; }Else{         returnCHS; }}

The above function is basically correct! But the test data is not many, if there is a problem convenient leave a word, so as to correct! Thank you!

JS calculate the past and future time distance now how many days?

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.