TypeScript

Date Range Generator

admin by @admin ADMIN
5h ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
Yield each day (or step) between two dates as a Date object. Generator-based so consumers can `break` early without computing the whole range.
TypeScript
Raw
export function* dateRange(
  start: Date,
  end: Date,
  stepMs: number = 86_400_000,
): Generator<Date> {
  if (stepMs <= 0) throw new Error('stepMs must be positive');
  for (let t = start.getTime(); t <= end.getTime(); t += stepMs) {
    yield new Date(t);
  }
}

for (const d of dateRange(new Date('2025-01-01'), new Date('2025-01-05'))) {
  console.log(d.toISOString().slice(0, 10));
}
// 2025-01-01
// 2025-01-02
// 2025-01-03
// 2025-01-04
// 2025-01-05
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.