Deviopath › JavaScript

Recurring Dates

Hard · Dates

Write a function recurringDates(start, every, unit, count) that lists the first count dates of a repeating event. start is a "YYYY-MM-DD" string and unit is "day", "week", "month" or "year".

- The list starts with start, then adds every units each time.

- For months and years, keep the day of start. If a month is too short, use its last day, but do not let that shift later dates: from "2024-01-31" monthly you get "2024-02-29", then "2024-03-31".

- Mind leap years.

- Return "YYYY-MM-DD" strings. Use UTC methods (Date.UTC, getUTCDate...) or plain arithmetic so the time zone never changes the result.

Next: Parse a Duration