Skip to content

Home

Date range generator in JavaScript

Generating a range of Date values is very common when working with any type of data that involves dates. Luckily, ES6 introduced generators, which can be used to create a generator function that yields all dates in a given range, allowing us to save memory and time.

šŸ’¬ Note

If you're not familiar with generator functions, be sure to read the range generator snippet first.

As mentioned in a previous post, we can manipulate Date objects using Date.prototype.getDate() and Date.prototype.setDate(). This allows us to easily increment or decrement dates.

Knowing that, we can construct a generator function that uses a for loop to iterate over the dates in the given range, incrementing by a specified step, allowing us to yield each date in the range.

const dateRange = function* (start, end, step = 1) {
  for (
    let d = new Date(start);
    d < new Date(end);
    d.setDate(d.getDate() + step)
  )
    yield new Date(d);
};

[...dateRange('2021-06-01','2021-06-04')];
// [ 2021-06-01, 2021-06-02, 2021-06-03 ]

More like this

Start typing a keyphrase to see matching snippets.