Saturday, February 2, 2019

Add Business Days in SuiteScript

Standard SuiteScript API nlapiAddDays adds or subtracts a number of days to or from a Date object, including weekends on the count.

The function below provides the ability to add only the business days from a Date object.



          function addBusinessDays(baseDate, daysToAdd) {
             var newDate = baseDate;
             var bussDayCounter = 0;

             while ( bussDayCounter < daysToAdd ) {
                      newDate = nlapiAddDays(newDate, 1);
                      if ( newDate.getDay() == 0 ) {
                               newDate = nlapiAddDays(newDate, 1);
                      } else if ( newDate.getDay() == 6 ) {
                               newDate = nlapiAddDays(newDate, 2);
                      }

                      bussDayCounter++;
             }

             return newDate;
          }

For example:
          var newDay = addBusinessDays(new Date("7/17/2012"), 17);

Returns Date object - Thu Aug 09 2012 00:00:00 GMT-0700 (PDT).
         

2 comments:

  1. I have been reading out many of your articles and i must say pretty good stuff. I will surely bookmark your site best logo designing company

    ReplyDelete
  2. Keep it up!! You have done the nice job having provided the latest information.Small Business ERP Software

    ReplyDelete