Menu

gitpiper

dateRangeGenerator javascript Code Snippet in 2024

datefunctiongeneratoradvanced

Last Updated: 22 July 2024

Creates a generator, that generates all dates in the given range using the given step.

  • Use a while loop to iterate from start to end, using yield to return each date in the range, using the Date constructor.
  • Use Date.prototype.getDate() and Date.prototype.setDate() to increment by step days after returning each subsequent value.
  • Omit the third argument, step, to use a default value of 1.
const dateRangeGenerator = function* (start, end, step = 1) { let d = start; while (d < end) { yield new Date(d); d.setDate(d.getDate() + step); } };
[...dateRangeGenerator(new Date('2021-06-01'), new Date('2021-06-04'))]; // [ 2021-06-01, 2021-06-02, 2021-06-03 ]

javascript snippet similar to dateRangeGenerator For You in July 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️