Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session

Calculate the number of week days between two dates considering Friday and Saturday as weekends

Avatar

Level 1

How can I calculate the number of week days between two dates considering Friday and Saturday as weekends.

Thanks & Regards

Shyam

2 Replies

Avatar

Level 10

Hi Shyam,

Using JavaScript you could use the following function, which loops though from the start date to the end date and uses the standard getDay function to determine which day of the week we are dealing with.

function CalculateBusinessDays(startDate, endDate, weekend)

{

  var result = 0;

  for (var currentDate = startDate; currentDate.getTime() < endDate.getTime(); currentDate.setDate(currentDate.getDate()+1))

  {

    if (weekend.indexOf(currentDate.getDay()) < 0)

    {

result++;

    }

  }

  return result;

}

If you have two DateTimeFields then you can call this function using the following code

var startDate = util.scand("yyyy-mm-dd", StartDate.rawValue);

startDate = new Date(startDate.setHours(0,0,0,0)); // ensure we are are not dealing with any time values.

var endDate = util.scand("yyyy-mm-dd", FinishDate.rawValue);

endDate = new Date(endDate.setHours(0,0,0,0));

var Days = { Sunday : 0, Monday : 1, Tuesday : 2, Wednesday : 3, Thursday : 4, Friday : 5, Saturday : 6 };

BusinessDays.rawValue = CalculateBusinessDays(startDate, endDate, [Days.Friday, Days.Saturday]);

Regards

Bruce