如何从结果集中跳过假期? [英] How to skip Holidays from the set of result?

查看:68
本文介绍了如何从结果集中跳过假期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否也可以跳过假期?假设我在数据库中设置了假期列表.假日不会出现在结果日期.我想跳过假期日期并跳到下一个日期就可以了

如果假期是2019年2月5日,则跳过此日期,需要显示2019年2月6日(不包括星期五).那么接下来的30天将是2019年3月13日(从2月6日开始计算的30天)

例如

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

现在,代码结果类似于

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

期待假期的到来

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> 

解决方案

您是这个意思吗?

 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> 

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>

解决方案

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>

这篇关于如何从结果集中跳过假期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆