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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…