SHARE
TWEET

Untitled

a guest Sep 11th, 2019 92 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function addBusinessTime(holiday, time, duration) {
  2.     let finalTime = new Date(time.getTime());
  3.     finalTime.setSeconds(finalTime.getSeconds() + duration);
  4.  
  5.     // First lets cover all possible cases when the start time is before the
  6.     // holiday start.
  7.     if (time.getTime() <= holiday.start.getTime()) {
  8.         // If duration is negative or final time still before the holiday starts
  9.         // then there is nothing to do, simply return the final time.
  10.         if (duration <= 0 || (finalTime.getTime() <= holiday.start.getTime())) {
  11.             return finalTime;
  12.         }
  13.  
  14.         // Ok... final time is after the holiday starts, we have to calculate
  15.         // the new final time by skipping the holiday.
  16.         const extraTime = finalTime.getTime() - holiday.start.getTime();
  17.         return new Date(holiday.end.getTime() + extraTime);
  18.     }
  19.    
  20.     // This block covers all the possible cases where the start time is during
  21.     // the holiday.
  22.     if (time.getTime() >= holiday.start.getTime() && time.getTime() <= holiday.end.getTime()) {
  23.         // When duration is positive the new start time is the holiday end,
  24.         // otherwise, it is the holiday start, then we add the duration.
  25.         finalTime = new Date(duration >= 0 ? holiday.end.getTime() : holiday.start.getTime());
  26.         finalTime.setSeconds(finalTime.getSeconds() + duration);
  27.         return finalTime;
  28.     }
  29.    
  30.     // At this point we know the start time is after the holiday...
  31.  
  32.     // If duration is positive or the final time is after the holiday end we
  33.     // can return the final time.
  34.     if (duration >= 0 || (finalTime.getTime() >= holiday.end.getTime())) {
  35.         return finalTime;
  36.     }
  37.  
  38.     // Finally... we know start time is after the holiday and final time
  39.     // is before or during the holiday, in this case we need to calculate
  40.     // the final time by skipping the holiday.
  41.     const difference = finalTime.getTime() - holiday.end.getTime();
  42.     return new Date(holiday.start.getTime() + difference);
  43. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top