【问题标题】:How to create date occurrence between two dates?如何在两个日期之间创建日期发生?
【发布时间】:2020-01-31 16:07:15
【问题描述】:
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);

【问题讨论】:

  • 你已经评论了大部分答案,增加了额外的要求。请点击您问题下方的编辑链接,并确保问题中包含所有要求。

标签: javascript jquery date datepicker


【解决方案1】:

我在您的代码中发现了一些问题。

  1. 您在检查/打印之前增加日期。
  2. numWorkDayscurrentDate 不同。
  3. 你还有numWorkDays &lt; 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

【讨论】:

    【解决方案2】:

    这应该很好用

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

    【讨论】:

    • 先生不来了
    • 对不起,我的错,我编辑到正确的版本,请检查
    • 纯代码的答案没有那么有用。一个好的答案应该解释为什么 OP 有问题以及您的代码如何修复它。
    • 这在少数情况下不起作用,例如,如果startingDate 是星期五并且daysRemained 的突变与currentDate 不同。
    • @RobG,听起来很合理,但在这种特殊情况下,代码非常简单,我想它不言自明......至少我不喜欢有人开始解释我 3 行代码,我会把它当作羞辱......无论如何我明白你的意思,谢谢分享它
    【解决方案3】:

    要同时打印开始日期,您需要确保在第一次迭代时打印条件为真,并确保开始日期不是星期五(否则添加 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));
        }
    }
    

    【讨论】:

    • 我听不懂你在说什么?
    • 如果合同开始时间是 2019 年 9 月 1 日,结束时间是 2020 年 8 月 31 日..那么每个月我们需要做两次服务或工作
    • 所以当我给出期间 var period = 13;这些年服务在 b/w 中来了 25 次..这将是错误的?在一年的合同中,只有 24 服务应该来吗???
    • 我每个月需要做两次服务。所以一年12个月和24次服务会来吗??
    • 此对话无效。我只会重复您必须编辑您的问题并解释规则是什么。您没有在哪里解释过“每月三次”的参数。该问题仅涉及所选日期之间的天数。我现在继续前进。如果您不努力解释在问题中在一般情况下这些条件是什么(当三次?当两次?),那么不要期望任何我这边的努力。另请注意,在您的示例中(在问题中)有时每月有一个日期,有时是两个。您的问题没有一致性
    猜你喜欢
    • 1970-01-01
    • 2019-12-26
    • 1970-01-01
    • 1970-01-01
    • 2021-03-14
    • 1970-01-01
    • 1970-01-01
    • 2016-01-04
    • 1970-01-01
    相关资源
    最近更新 更多