DateJS - Do Not Include Weeknds

社会主义新天地 提交于 2019-12-13 07:46:34

问题


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>

回答1:


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?




回答2:


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]));
});


来源:https://stackoverflow.com/questions/41517796/datejs-do-not-include-weeknds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!