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?