Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

134
Views
Get array of dates between two dates with custom interval

I have this data:

const start = "29.09.2021";
const end = "29.10.2021";
const interval = "days"; //also: "month", "week", "year"
const intervalCount = 3;

how to get an array of dates that exists between start and end with intervals: eq. if interval == "days" and intervalCount == 3 then another dates should be 02.10, 05.10, 08.10, 11.10 etc., but interval can be also "month", "week", and "year" so I must calculate based on the dynamic arguments

I have no idea how to even start, thanks for any help!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This can be done using Date objects found here

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

You can create a function such as

function getDates(startDateString, stopDateString, interval){
  var dates = [];
  var start = new Date(startDateString);
  var stop = new Date(stopDateString);

  while (start < stop) {
    dates.push (start.toJSON());
    start.setDate(start.getDate() + interval);
  }
  return dates;
}

And then you can run the function by making this call

dateArray = getDates("09-29-2021", "10-29-2021", 1)

It covers single day iterations. A week is just a 7 day interval.

dateArray = getDates("09-29-2021", "10-29-2021", 7)

Moving months gets into edge cases that can be handled through conditions based on your use case (For example, if the date is Jan 31st, should a jump of 1 month take you to Mar 3rd or Feb 28th/29th?)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!