Trying to write a function in Typescript that returns the number of Wednesdays between 2 given dates and I'm stuck, any help would be greatly appreciated <3
Asked
Active
Viewed 105 times
0
-
2Does this answer your question? [Calculate number of specific weekdays between dates](https://stackoverflow.com/questions/25562173/calculate-number-of-specific-weekdays-between-dates) – Alex Brohshtut Jul 16 '22 at 06:42
1 Answers
0
If you're handling dates, I would advise you to use a library like Luxon.
With luxon you can do it the following way :
const start = DateTime.fromObject({ year: 2012, month: 10, day: 1 });
const end = DateTime.fromObject({ year: 2020, month: 5, day: 6 });
const interval = Interval.fromDateTimes(start, end);
const subIntervals = interval.splitBy({ days: 1 });
const wednesday = subIntervals.reduce((wednesdays, subInt) => {
const d = subInt.start;
return d.weekday == 3 ? wednesdays + 1 : wednesdays;
}, 0);

Matthieu Riegler
- 31,918
- 20
- 95
- 134