February 25, 2001 - Computing the Next Business Day | WebReference

February 25, 2001 - Computing the Next Business Day

Yehuda Shiran February 20, 2001
Computing the Next Business Day
Tips: February 2001

Yehuda Shiran, Ph.D.
Doc JavaScript

In many applications you need to find the next business day. Suppose you operate a purchasing and delivering site on the Web. Your operation model constrains your delivery service to send out every package the next business day. You probably want to tell your customers at which date the package would leave the premises. A bank processes customer applications for a new account in one business day. The bank would like to inform applicants when should they check back about their applications. These are typical examples where computing the next business day is very handy. The following function, getNextBusinessDay(), computes the next business day. We take advantage of our function, getDeliveryDateObj(1), which forward-computes the date, counting business days only. We just pass the value of 1 to this function (national holidays are counted as business days):

<SCRIPT LANGUAGE="JavaScript">
function getNextBusinessDay() {
  return getDeliveryDateObj(1);
}
function getDeliveryDateObj(businessDaysLeftForDelivery) {
  var now = new Date();
  var dayOfTheWeek = now.getDay();
  var calendarDays = businessDaysLeftForDelivery;
  var deliveryDay = dayOfTheWeek + businessDaysLeftForDelivery;
  if (deliveryDay >= 6) {
    businessDaysLeftForDelivery -= 6 - dayOfTheWeek;  //deduct this-week days
	calendarDays += 2;  //count this coming weekend
	deliveryWeeks = Math.floor(businessDaysLeftForDelivery / 5); //how many whole weeks?
	calendarDays += deliveryWeeks * 2;  //two days per weekend per week
  }
  now.setTime(now.getTime() + calendarDays * 24 * 60 * 60 * 1000);
  return now;
}
</SCRIPT>

Click here to find out the next business day.