如何创建两个日期之间的日期出现?

时间:2019-10-02 11:22:35

标签: javascript jquery date datepicker

Start Date: 2019-03-03

End Date: 2020-03-02

开始日期和结束日期之间的15天间隔不包括所有星期五

确切的预期产量不包括所有星期五

20-Mar-2019 //i need to start with the date 2019-03-03 dont start with the 20 March 2019
07-Apr-2019
24-Apr-2019
12-May-2019
29-May-2019
16-Jun-2019
03-Jul-2019
21-Jul-2019
07-Aug-2019
25-Aug-2019
11-Sep-2019
29-Sep-2019
16-Oct-2019
03-Nov-2019
20-Nov-2019
08-Dec-2019
25-Dec-2019
12-Jan-2020
29-Jan-2020
16-Feb-2020

在我的代码中,它不会在2019年3月20日到来(开始日期是指所给时间)。请建议我在代码中有误的地方。

http://jsfiddle.net/2mjshr1d/

功能代码

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 firtsjobstart = new Date("2019-03-03");
var end = new Date("2020-03-02");
var period = 157;
printNextPeriod(firtsjobstart, end, period);

3 个答案:

答案 0 :(得分:1)

我在您的代码中发现了几个问题。

  1. 您可以在检查/打印日期之前增加日期。
  2. numWorkDayscurrentDate的突变方式不同。
  3. 您也有错误的numWorkDays < periodInDays

为解决此问题,我修改了您的printNextPeriod函数,将其命名为printNextPeriods

function printNextPeriod(startDate, endDate, periodInDays) {
    var numWorkDays = 0;
    var currentDate = new Date(startDate);
    while (currentDate <= endDate) { // only this condition is enough as you're modifying the currentDate
        // Skips friday
        if (currentDate.getDay() == 5) { // increase the currentDate and the numWorkDays if the currentDay is friday.. 
            currentDate = currentDate.addDays(1);
            numWorkDays++;
        }
        if (0 == numWorkDays % periodInDays) { //if its the same day or the periodInDays'th day..
            numWorkDays = 0;
            cnt++; 
            document.getElementById("first").innerHTML += dateToYMD(currentDate)+"<br/>";
            document.getElementById("second").innerHTML += cnt+nth(cnt)+(cnt==1?" Basic":" Control")+ " Treatment"+"<br/>";

        }
        numWorkDays ++;
        currentDate = currentDate.addDays(1);


    }
}

这是更新后的jsfiddle

答案 1 :(得分:0)

这应该很好

function printNextPeriod(startDate, endDate, periodInDays) {
  var daysRemained = 0;
  var currentDate = new Date(startDate);
  while (currentDate <= endDate) {
    if (daysRemained === 0) {
      cnt++;
      daysRemained = periodInDays
      document.getElementById("first").innerHTML += dateToYMD(currentDate)+"<br/>";
      document.getElementById("second").innerHTML += cnt+nth(cnt)+(cnt==1?" Basic":" Control")+ " Treatment"+"<br/>";  
    }
    // Skips friday
    if (currentDate.getDay() !== 5) {
      daysRemained --;
    }
    currentDate = currentDate.addDays(1);
  }
}

答案 2 :(得分:0)

要同时打印开始日期,您需要确保在第一次迭代中要打印的条件为true,并确保开始日期不是星期五(否则增加1天)。

作为进一步的改进,我不会重复使用.addDays(1),而是会进行一次数学运算以得出下一个日期。为此,您可以确定间隔时间段中可以容纳多少整周,然后使用剩余的天数来查看是否跨越了星期五。

这是建议的代码:

function printNextPeriod(startDate, endDate, periodInDays) {
    var fullWeeks = Math.floor(periodInDays / 6); // number of weeks that fit in period
    var numWorkDays = periodInDays - fullWeeks * 6; // remaining number of days
    var currentDate = new Date(startDate);
    // Make sure not to start on a Friday
    if (currentDate.getDay() === 5) currentDate = currentDate.addDays(1);

    while (currentDate <= endDate) {
        cnt++; 
        document.getElementById("first").innerHTML += dateToYMD(currentDate)+"<br/" + ">";
        document.getElementById("second").innerHTML += cnt+nth(cnt)+(cnt==1?" Basic":" Control")+ " Treatment"+"<br/" + ">";

        var daysUntilFriday = (12 - currentDate.getDay()) % 7;
        // Add the full weeks (7 days, always bringing you to the same day-of-the-week)
        // Add the remaining number of days
        // Add one extra day if we cross a Friday in that remaining number of days
        currentDate = currentDate.addDays(fullWeeks * 7 + numWorkDays 
                                          + (numWorkDays >= daysUntilFriday));
    }
}