如何通过给天间隙创建两个日期之间的日期出现?

时间:2019-08-20 23:07:11

标签: javascript jquery date datepicker

我有以下数据:

1年合同

Start Date: 01-01-2019

End Date: 31-12-2019

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

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

19-01-2019 //exclude Fridays then got 19 jan 2019
-----------
05-02-2019 //after 15 days
-----------
23-02-2019 //after 15 days
-------------
keep on adding.. hit until end month 12-2019

如何生成?有更好的方法吗?

var start = new Date("2019-01-01");
var end = new Date("2019-12-31");

while (start <= end) {
    console.log( new Date(start) );
    start.setMonth( start.getMonth() + 1 );
}

1 个答案:

答案 0 :(得分:-1)

在您的用例中,星期五似乎是一个工作日,您想根据开始日期打印第15天。

Date.prototype.addDays = function(days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date; }

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;
        console.log(currentDate);
    }
} }

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