简体   繁体   中英

How to create date occurrence between two dates by giving days gap?

I have the below data:

1 year contract

Start Date: 01-01-2019

End Date: 31-12-2019

15 days gap between start and end date exclude all Fridays

Exact Expected output exclude all Fridays

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

How to generate it?Is there a better approach for doing this?

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

from your use case, it seems that Friday is a non-working day and you would like to print the 15th day based on the start date.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM