Let's assume that there are two dates: 2022-01-10 and 2022-08-20.
User can type a day of a month when an action needs to be executed repeatedly once per month between these dates.
So if user types "25" the action should be executed 7 times, because the end date is out of range.
How to get such result using luxon in JavaScript? I need to have number of matching dates OR (that would be even better) a list of these dates.
Another example:
Input:
Day of a month: 5
Start date: 2022-07-08
End date: 2022-09-05
Expected output:
2022-08-05
2022-09-05
Thanks in advance.
A little hacky but this should get you on a decent path...
const startDate = luxon.DateTime.utc(2022, 1, 5, 0, 0, 0, 0)
const endDate = luxon.DateTime.utc(2022, 6, 5, 0, 0, 0, 0)
const diff = endDate.diff(startDate, ["months"])
// we get a diff of `5` here, let's generate the date map next
console.log(diff.toObject())
const possibleDates = []
const dayOfMonthInput = 2
const targetDateByInput = luxon.DateTime.utc(2022, 1, dayOfMonthInput, 0, 0, 0, 0)
for (
let i = 0,maybeDate=targetDateByInput;
i <= diff.months;
i++, maybeDate = targetDateByInput.plus({ months: i })) {
// is maybeDate valid after START date?
if (maybeDate.startOf('day') > startDate.startOf('day')) {
// is maybeDate valid BEFORE end date?
if (maybeDate.startOf('day') < endDate.startOf('day')) {
// add to collection
possibleDates.push(maybeDate)
}
}
}
// output possible dates
console.log(possibleDates)
<script src="https://cdn.jsdelivr.net/npm/luxon@1.25.0/build/global/luxon.min.js"></script>