Skip to content

2777. Date Range Generator 👍

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
// Similar to 2758. Next Day
function* dateRangeGenerator(
  start: string,
  end: string,
  step: number
): Generator<string> {
  const startDate = new Date(start);
  const endDate = new Date(end);
  let currentDate = startDate;
  while (currentDate <= endDate) {
    yield currentDate.toISOString().split('T')[0];
    currentDate.setDate(currentDate.getDate() + step);
  }
}