DateJS - Do Not Include Weeknds

188 views Asked by At

I have DateJS set up on my website to give users the ability to know when they will receive their package.

I am utilizing DateJS script to do this.

I need to modify the script so that it does not include weekends when estimating the delivery day.

For example, today (Friday), if someone selects one of the following:

Next Day Air: it should show Monday, not Saturday Two Day Air: It should show Tuesday, not Sunday Three Day Air: It should show Wednesday, not Monday

Help is greatly appreciated.

<div class="panel-body">
     <table class="table table-striped">
        <thead>
          <th>Shipping Method</th>
          <th>Estimated Delivery</th>
        </thead>
        <tbody>
          <tr>
            <td>3 Day Select</td>
            <td><span class="fromDate-1"></span>
             <script>
               var fromDate = Date.today().addDays(3);
                if (fromDate.is().saturday() || fromDate.is().sunday())
                 fromDate = fromDate.next().monday();
                 $('.fromDate-1').html(fromDate.toString('dddd MMMM dS'));
             </script>
           </td>
          </tr>
          <tr>
            <td>2nd Day Air</td>
            <td><span class="fromDate-2"></span>
              <script>
                var fromDate = Date.today().addDays(2);
                if (fromDate.is().saturday(1) || fromDate.is().sunday())
                fromDate = fromDate.next().monday();
                $('.fromDate-2').html(fromDate.toString('dddd MMMM dS'));
              </script>
            </td>
          </tr>
          <tr>
            <td>Next Day Air</td>
            <td>
               <span class="fromDate-3"></span>
               <script>
                  var fromDate = Date.today().addDays(1);
                  if (fromDate.is().saturday() || fromDate.is().sunday())
                   fromDate = fromDate.next().monday();
                   $('.fromDate-3').html(fromDate.toString('dddd MMMM dS'));
               </script>
              </td>
             </tr>
          </tbody>
       </table>
    </div>
2

There are 2 answers

0
Cameron McFee On

You're only looking at weekends for the final date after adding days, not looking at weekends as the start or (in the case of 3) in between days. Why not a function like

function addDays(date, daysToAdd) {
    for (let i=0; i<daystoAdd; i++) {
        let nextDay = date.addDays(1);
        if (nextDay.is().saturday()) {
            nextDay = date.next().monday();
        }
        date = nextDay;
    }
 return date;
 }

Then you just need to call that function for each of your spans:

$('.fromDate-1').html(addDays(Date.today(), 3));

Is that the problem you were trying to solve?

0
RobG On

I wouldn't use a date library at all for this, it's very straight forward.

It seems goods only travel on business days, so you need a function that adds business days rather than your current algorithm, something like the answers to exclude weekends in javascript date calculation.

The following does something similar to just add business days, it's only efficient for a small number of days (say 0 to 10).

/* Add business days to a date.
** @param {Date} date - date to start from
** @param {number} deliveryDays - positive integer
** @returns {Date} a new Date object for the delivery date
*/
function getDeliveryDate(date, deliveryDays) {
  // Default delivery is 3 days
  deliveryDays = typeof deliveryDays == 'undefined'? 3 : deliveryDays;
  // Copy date so don't modify original
  var d = new Date(+date);
  // Add days. If land on a Saturday or Sunday, move another day
  while (deliveryDays--) {
    d.setDate(d.getDate() + 1);
    if (!(d.getDay() % 6)) ++deliveryDays;
  }
  return d;
}
 
// Some examples
[
 [new Date(2017,0,7),3],  // Saturday start, 3 day delivery
 [new Date(2017,0,8),1],  // Sunday start, 1 day delivery
 [new Date(2017,0,13),1], // Friday start, 1 day delivery
 [new Date(2017,0,13),3], // Friday start, 3 day delivery
 [new Date(2017,0,9), 3], // Monday start, 3 day delivery
 [new Date(2017,0,8), 7]  // Sunday start, 7 day delivery
].forEach(function(d) {
  console.log('Ordered day  : ' + d[0].toString() + 
              '\nDelivery days: ' + d[1] +
              '\nDelivery on  : ' + getDeliveryDate(d[0], d[1]));
});