I have this data:
const start = "29.09.2021";
const end = "29.10.2021";
const interval = "days"; //also: "month", "week", "year"
const intervalCount = 3;
how to get an array of dates that exists between start and end with intervals: eq. if interval == "days" and intervalCount == 3 then another dates should be 02.10, 05.10, 08.10, 11.10 etc., but interval can be also "month", "week", and "year" so I must calculate based on the dynamic arguments
I have no idea how to even start, thanks for any help!
This can be done using Date objects found here
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
You can create a function such as
function getDates(startDateString, stopDateString, interval){
var dates = [];
var start = new Date(startDateString);
var stop = new Date(stopDateString);
while (start < stop) {
dates.push (start.toJSON());
start.setDate(start.getDate() + interval);
}
return dates;
}
And then you can run the function by making this call
dateArray = getDates("09-29-2021", "10-29-2021", 1)
It covers single day iterations. A week is just a 7 day interval.
dateArray = getDates("09-29-2021", "10-29-2021", 7)
Moving months gets into edge cases that can be handled through conditions based on your use case (For example, if the date is Jan 31st, should a jump of 1 month take you to Mar 3rd or Feb 28th/29th?)