Pizush
Pizush

Reputation: 33

Calculate days excluding weekend and holiday in JavaScript

I am trying to write a code where total days will be counted excluding weekends and custom defined holiday. I searched through stackoverflow and adobe forum to find a solution and came with below code. If public holiday falls in a working day (Saturday-Wednesday) it is excluding from calculation. My problem is that if public holiday falls in weekend (Thursday-Friday), it is deducting for both (holiday & weekend). Suppose leave duration is 18/09/2018-22/09/2018, total count 2 days is showing in place of 3. Again for 17/10/2018-21/10/2018, total count 1 day is showing in place of 3 days. Any help or any idea to solve the problem would be great! Regards

//Thursday and Friday will be excluded as weekend.
var start = this.getField("From").value;
// get the start date value
var end = this.getField("To").value;
var end = util.scand("dd/mm/yyyy H:MM:SS", end + " 0:00:00");
var start =util.scand("dd/mm/yyyy H:MM:SS", start + " 0:00:00");
event.value = dateDifference(start, end);
function dateDifference(start, end) {
  // Copy date objects so don't modify originals
  var s = new Date(+start);
  var e = new Date(+end);
  // Set time to midday to avoid daylight saving and browser quirks
  s.setHours(12,0,0,0);
  e.setHours(12,0,0,0);
  // Get the difference in whole days
  var totalDays = Math.round((e - s) / 8.64e7);
  // Get the difference in whole weeks
  var wholeWeeks = totalDays / 7 | 0;
  // Estimate business days as number of whole weeks * 5
  var days = wholeWeeks * 5;
  // If not even number of weeks, calc remaining weekend days
  if (totalDays % 7) {
    s.setDate(s.getDate() + wholeWeeks * 7);
    while (s < e) {
      s.setDate(s.getDate() + 1);
      // If day isn't a Thursday or Friday, add to business days
      if (s.getDay() != 4 && s.getDay() != 5) {
        ++days;
      }
    }
  }
var hdayar = ["2018/02/21","2018/03/17","2018/03/26","2018/04/14","2018/05/01","2018/08/15","2018/09/2 1","2018/10/18","2018/10/19","2018/12/16","2018/12/25"];
//test for public holidays
var phdays = 0;
for (var i = 0; i <hdayar.length; i++){
if ((Date.parse(hdayar[i]) >= Date.parse(start)) && (Date.parse(hdayar[i]) <= Date.parse(end))) {phdays ++;}}
  return days-phdays + 1;
}

Upvotes: 3

Views: 8263

Answers (2)

Avinash Singh
Avinash Singh

Reputation: 5396

Just have the same requirement and this is the my work around.Hope it helps other

var holiday = ["4/18/2019", "4/19/2019", "4/20/2019", "4/25/2019", "4/26/2019"];
var startDate = new Date();
var endDate = new Date(startDate.setDate(startDate.getDate() + 1));

for (i = 0; i < holiday.length; i++) {
    var date = endDate.getDate();
    var month = endDate.getMonth() + 1; //Months are zero based
    var year = endDate.getFullYear();
    if ((month + '/' + date + '/' + year) === (holiday[i])) {
        endDate = new Date(endDate.setDate(endDate.getDate() + 1));
        if (endDate.getDay() == 6) {
            endDate = new Date(endDate.setDate(endDate.getDate() + 2));
        } else if (endDate.getDay() == 0) {
            endDate = new Date(endDate.setDate(endDate.getDate() + 1));
        }
    }
}

Here, end date gives you next working day.Here,I'm ignoring current day and start comparing from Next day whether it's holiday or weekend.You can customize dateTime as per your requirement (month + '/' + date + '/' + year).Careful whenever you compares two dates with each other. Because it looks same but actually it's not.So customize accordingly.

Upvotes: 0

Stuart
Stuart

Reputation: 9858

You should use a library for this rather than reinventing the wheel.

But if you want to do it yourself you could use .getDay to check if the public holidays are on a weekend.

var weekend = [4, 5],   // for Thursday, Friday
    holDate, holDay;
for (var i = 0; i < hdayar.length; i++){
    holDate = Date.parse(hdayar[i]);
    holDay = new Date(holDate).getDay()
    if (weekend.indexOf(holDay) == -1 && holDate >= Date.parse(start) && holDate <= Date.parse(end)) {
        phdays ++;
    }
}

phdays will now contain the number of non-weekend public holidays within the range.

Upvotes: 1

Related Questions