JS calculates the number of seconds between two time ranges

Source: Internet
Author: User
Tags ranges

Recently in a project, you need to calculate the number of seconds before the interval of two time periods, where the code is recorded. The code is very simple, mainly need logic clear, because there are several situations need to distinguish, put here to share under.

1 functionWeekend (time1,time2) {2         //calculate time intervals, excluding weekends and non-working hours3         if(time1!=NULL&&time2!=NULL){4             Debugger;5             vartimesstart1=Date.parse (time1);6             vardat1=NewDate (TIMESSTART1);7             varDat1str=dat1.getfullyear () + "-" + (Dat1.getmonth () +1) + "-" +dat1.getdate () + "" +dat1.gethours () + ":" +dat1.getminutes () +":"+dat1.getseconds ();8             varDay1=dat1.getfullyear () + "-" + (Dat1.getmonth () +1) + "-" +dat1.getdate () + "17:30:00";//17:30 on the day of the issued time9             if(Date.parse (DAT1STR) >Date.parse (Day1)) {Ten                 varentdays=54000000;//time stamp from 17:30 to 8:30 OneTimesstart1=date.parse (day1) +entdays;//If the release time is greater than the off-hours, the release time to change to the next day of the A             } -             varTimeend2=date.parse (time2) +1000; -             varDat2=NewDate (timeend2); the             varDay2=dat2.getfullyear () + "-" + (Dat2.getmonth () +1) + "-" +dat2.getdate () + "08:30:00";//on the day of accepting tasks -             varformatday1=Date.parse (day1); -             varFormatday2=Date.parse (day2); -             if(Day1.substring (0,10) ==day2.substring (0,10)){ +                 return((TIMEEND2-TIMESSTART1)/1000);//release time and acceptance time is the same day -}Else{ +                 varTodayss=formatday1-timesstart1;//The number of seconds to 17:30 in the issued time A                 varTomorrowss=timeend2-formatday2;//The number of seconds to accept the time to 08:30 at                 vardays=86400000; -                 vardaywork=32400000;//number of seconds in a day -                 varIntervals= (Timeend2-timesstart1)/days; -                 varcnt=intervals/7;//a few whole weeks -                 varmdays=intervals-7*cnt;//The remaining days -cnt=cnt*2;//There are two days in a week, n*2 days in n weeks. in                 varn=NewDate (time1). GetDay ();//What's the day of the week? -                 if(n+mdays>7){ toCnt=cnt+2; +}if(n+mdays==7){ -cnt++; the                 } *                 varDayn=math.floor (INTERVALS-CNT);//A few days $                 return((TODAYSS+DAYN*DAYWORK+TOMORROWSS)/1000);Panax Notoginseng             } -}Else return0 ; the}

JS calculates the number of seconds between two time ranges

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.