Sorry for the sloppily worded title, but here is an example of what I want to achieve:
//increment of 5
const daysOfMonthArray = [16, 21, 26, 31, 5, 10, 15, 20, 25, 30, 4, 9.....]
I’m having a hard time trying to find a good way to do this using dayJS, but I’m sure it’s reasonably doable with JavaScript’s built in date objects as well. Any help would be greatly appreciated, thanks!
4
Answers
Use
Date::getDate()
andDate::setDate()
to manipulate the date part:You can use a generator function for this purpose:
Explanation: the generator function takes a date and a days parameter and (forever) adds the number of days to the date, always yielding the actual result.
Here is a slight variation of the answer from Alexander, where the loop has no side-effects (on a variable
dt
):Use
Array.from()
to create an array with the specified length. The day value for each element is calculated in the mapping function by adding the starting day to the index multiplied by the increment.