Calculates the number of non-weekend days between 2 dates using Suitescript

The below code snippet will help you to calculate the number of non-weekend days (business days) between two dates using SuiteScript.

/**
 * Calculates the number of non-weekend days between two dates in SuiteScript.
 * @param {Date} startDate - The starting date.
 * @param {Date} endDate - The ending date.
 * @returns {number} - The number of non-weekend days.
 */
function getBusinessDays(startDate, endDate) {
  const millisecondsPerDay = 24 * 60 * 60 * 1000;
  let businessDays = 0;

  const currentDate = new Date(startDate);

  while (currentDate <= endDate) {
    const dayOfWeek = currentDate.getDay(); // Sunday: 0, Monday: 1, ..., Saturday: 6

    if (dayOfWeek !== 0 && dayOfWeek !== 6) {
      businessDays++;
    }

    currentDate.setTime(currentDate.getTime() + millisecondsPerDay);
  }

  return businessDays;
}

// Example usage
const startDate = new Date('2023-08-15'); // Replace with your start date
const endDate = new Date('2023-08-30');   // Replace with your end date

const businessDays = getBusinessDays(startDate, endDate);
console.log(`Number of business days: ${businessDays}`);

Leave a comment

Your email address will not be published. Required fields are marked *