Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
119 views
in Technique[技术] by (71.8m points)

javascript - 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 date is fine

if holiday is 05-Feb-2019 then skip this date and need to show 06-Feb-2019(exclude Fridays)..Then next 30 days will be 13-Mar-2019 (30days calculating from 06 Feb)

for example

var holidays = array("05-Feb-2019", "08-Oct-2019", "17-Dec-2019"); 

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;
}

var cnt = 0;
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++; 
      document.getElementById("first").innerHTML += dateToYMD(currentDate)+"<br/>";
      document.getElementById("second").innerHTML += cnt+nth(cnt)+(cnt==1?" Basic":" Control")+ " Treatment"+"<br/>";

    }
  }
}

var start = new Date("2019-01-01");
var end = new Date("2019-12-31");
var period = 30;
printNextPeriod(start, end, period);

Now the code results like

**Date**
05-Feb-2019   
12-Mar-2019
-----------
12-Nov-2019
17-Dec-2019

Expecting output with skiping holidays

06-Feb-2019(exclude fridays)
13-Mar-2019
----------
so on

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;
}

var cnt = 0;

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++;
      document.getElementById("first").innerHTML += dateToYMD(currentDate) + "<br/>";
      document.getElementById("second").innerHTML += cnt + nth(cnt) + (cnt == 1 ? " Basic" : " Control") + " Treatment" + "<br/>";

    }
  }
}

var start = new Date("2019-01-01");
var end = new Date("2020-01-01");
var period = 30;
printNextPeriod(start, end, period);
.period {
  float: left;
  padding: 5px;
  text-align: right;
  font-family: monospace;
}
<div class="period" id="first">**Date**
  <hr/>
</div>
<div class="period" id="second">**Frequency**
  <hr/>
</div>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...