Skip to content Skip to sidebar Skip to footer

How To Skip Holidays From The Set Of Result?

Is it possible to skip Holidays also?? Assume i have set of holiday list in database..holidays will not comes to the result date.I want to skip the holiday date and skip to next da

Solution 1:

You mean this?

function nth(d)  { if (d > 3 && d < 21) return 'th';  switch (d % 10) { case 1:  return "st"; case 2:  return "nd"; case 3:  return "rd"; default: return "th"; } }
function dateToYMD(date) { var strArray=['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; var d = date.getDate(); var m = strArray[date.getMonth()]; var y = date.getFullYear(); return '' + (d <= 9 ? '0' + d : d) + '-' + m + '-' + y; }
Date.prototype.addDays = function(days) { var date = new Date(this.valueOf()); date.setDate(date.getDate() + days); return date; }
function pad(str) { return (" "+str).slice(-2) }
var cnt = 0, dataSet = [];

function printNextPeriod(startDate, endDate, periodInDays) {
  var numWorkDays = 0;
  var currentDate = new Date(startDate);
  while (numWorkDays < periodInDays && currentDate <= endDate) {
    currentDate = currentDate.addDays(1);
    // Skips friday
    if (currentDate.getDay() !== 5) {
      numWorkDays++;
    }
    if (numWorkDays == periodInDays) {
      numWorkDays = 0;
      cnt++;
      let date = dateToYMD(currentDate);
      let pos = holidays.indexOf(date);
      if (pos != -1) {
        console.log("replace",date,"with",instead[pos])
        date = instead[pos];
      }  
      let treatment = pad(cnt) + nth(cnt) + (cnt == 1 ? " Basic" : " Control") + " Treatment"
      dataSet.push([date, treatment])
    }
  }
}

var holidays = ["2019-02-07" // Thursday
  , "2019-10-08", "2019-12-17",
  "2019-02-05" // Friday
];
var instead = [];
holidays.forEach((hol,i) => {
  let d = new Date(hol);
  let date = d.getDate() + 1
  d.setDate(date); // next day (could be Weekend);
  while (d.getDay() === 5) {
    date = d.getDate()
    date++;
    d.setDate(date); // is any day not friday ok?
  }
  instead.push(dateToYMD(d))
  holidays[i] = dateToYMD(new Date(hol))
})
console.log(JSON.stringify(instead))
var start = new Date("2018-12-15");
var end = new Date("2019-12-31");
var period = 15;
printNextPeriod(start, end, period);

$(document).ready(function() {
  $('#example').DataTable({
    data: dataSet,
    columns: [{ title: "Date" },
              {title: "Frequency"}],
    order: [[1, "asc"]]
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" />
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<table id="example">
  </div>

Post a Comment for "How To Skip Holidays From The Set Of Result?"