869. Date Range Generator

Given a start date start, an end date end, and a positive integer step, return a generator object that yields dates in the range from start to end inclusive.

The value of step indicates the number of days between consecutive yielded values.

All yielded dates must be in the string format YYYY-MM-DD.

Example 1:

Input : start = "2023-09-01" , end = "2023-09-08", steps = 2

Output : [ "2023-09-01", "2023-09-03", "2023-09-05", "2023-09-07" ]

Explanation :

const g = dateRangeGenerator(start, end, step);

g.next().value // '2023-09-01'

g.next().value // '2023-09-03'

g.next().value // '2023-09-05'

g.next().value // '2023-09-07'

Example 2:

Input : start = "2025-02-20" , end = "2025-02-24", steps = 1

Output : [ "2025-02-20", "2025-02-21", "2025-02-22", "2025-02-23", "2025-02-24" ]

Explanation :

const g = dateRangeGenerator(start, end, step);

g.next().value // '2025-02-20'

g.next().value // '2025-02-21'

g.next().value // '2025-02-22'

g.next().value // '2025-02-23'

g.next().value // '2025-02-24'

Now Your Turn!

Pick the correct output for the given input

Input : start = "2020-01-01", end = "2024-01-01", steps = 365

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • new Date(start) <= new Date(end)
  • start and end dates are in the string format YYYY-MM-DD
  • 0 <= The difference in days between the start date and the end date <= 1500
  • 1 <= step <= 1000

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
*generateDates(start, end, step) {
//your code goes here
}
}
Test Case

Input:

End
Start
Steps