0

I need to generate the series of all dates between two given dates. How ever I am not able to get desire output.

I tried using the below code. I gt an empty array.

function getDates(startDate, endDate) {
  var dates = [];
  var currentDate = new Date(startDate);

  while (currentDate <= endDate) {
    var final = currentDate.getFullYear() + '-' + (((currentDate.getMonth() + 1) < 10) ? '0' : '') + (currentDate.getMonth() + 1) + '-' + ((currentDate.getDate() < 10) ? '0' : '') + currentDate.getDate();
    dates.push(final);
    currentDate = currentDate.setDate(currentDate.getDate() + 1);
  }
  return dates;
};

When I execute console.log(getDates("2019-10-10","2019-11-20")), I get the result as empty array. I didn't get the series of dates as a result.

Rohith K N
  • 845
  • 6
  • 17

4 Answers4

1

As mentioned by @RobG, parsing date can yield different results hence consider using the following:

function parseDate(input) {
  var parts = input.split('-');
  return new Date(parts[0], parts[1] - 1, parts[2]);
}

function getDates(startDate, endDate) {
  var dates = [];
  var currentDate = parseDate(startDate);
  endDate = parseDate(endDate);

  while (currentDate <= endDate) {
    var final = currentDate.getFullYear() + '-' + (((currentDate.getMonth() + 1) < 10) ? '0' : '') + (currentDate.getMonth() + 1) + '-' + ((currentDate.getDate() < 10) ? '0' : '') + currentDate.getDate();
    dates.push(final);
    currentDate.setDate(currentDate.getDate() + 1);
  }
  return dates;
}

console.log(getDates("2019-10-10", "2019-11-20"));

Original Answer:

You could change endDate into Date type and not set currentDate as setDate is doing it for you:

function getDates(startDate, endDate) {
  var dates = [];
  var currentDate = new Date(startDate);
  endDate = new Date(endDate);

  while (currentDate <= endDate) {
    var final = currentDate.getFullYear() + '-' + (((currentDate.getMonth() + 1) < 10) ? '0' : '') + (currentDate.getMonth() + 1) + '-' + ((currentDate.getDate() < 10) ? '0' : '') + currentDate.getDate();
    dates.push(final);
    currentDate.setDate(currentDate.getDate() + 1);
  }
  return dates;
}

console.log(getDates("2019-10-10", "2019-11-20"));
shrys
  • 5,860
  • 2
  • 21
  • 36
  • Its working as expected. Thanks. – Shankar_programmer Sep 11 '19 at 05:23
  • glad it helped :) – shrys Sep 11 '19 at 05:25
  • Note that "2019-10-10" is parsed as UTC, but you're using local methods to format the date string. So for users west of Greenwich, "2019-10-10" will produce a "final" date of "2019-10-09". See [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Sep 11 '19 at 12:58
1

You have to call new Date() on line 8.

function getDates(startDate, endDate) {
  const dates = [];
  let currentDate = new Date(startDate);

  while (currentDate <= new Date(endDate)) {
    const final = currentDate.getFullYear() + '-' + (((currentDate.getMonth() + 1) < 10) ? '0' : '') + (currentDate.getMonth() + 1) + '-' + ((currentDate.getDate() < 10) ? '0' : '') + currentDate.getDate();
    dates.push(final);
    currentDate = new Date(currentDate.setMonth(currentDate.getMonth()+1))
  }
  return dates;
};

const dates = getDates("2019-01-01", "2019-10-01");
console.log(dates);
Joven28
  • 769
  • 3
  • 12
1

As others have said, you're comparing a string and a Date, so things go awry.

ISO 8601 format dates can be compared as strings without being parsed to Dates. Timestamps in the format YYY-MM-DD are parsed as UTC, so you need to be careful with manipulating them. In the OP, the strings are parsed as UTC but local methods are used to format the timestamps, so they may be out by 1 day for users west of Greenwich.

One option is to use UTC methods for incrementing the date and to create strings for comparison, e.g.

// startDate, endDate in format YYYY-MM-DD
function getDates(startDate, endDate) {
  let toISODate = date => date.toISOString().substr(0,10);
  var dates = [];
  var currentDate = new Date(startDate);

  while (startDate <= endDate) {
    dates.push(startDate);
    currentDate.setUTCDate(currentDate.getUTCDate() + 1);
    startDate = toISODate(currentDate);
  }
  return dates;
};

console.log(getDates('2019-09-01', '2019-10-01'));
RobG
  • 142,382
  • 31
  • 172
  • 209
0

Use a library like moment.js for date manipulation. These functions are readily available in these.

window['moment-range'].extendMoment(moment);

const start = new Date("11/30/2018"), end = new Date("09/30/2019")
const range = moment.range(moment(start), moment(end));

console.log(Array.from(range.by('day')))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-range/4.0.1/moment-range.js"></script>

And this question has lot of other methods as answers - from which I copied the above solution.

Charlie
  • 22,886
  • 11
  • 59
  • 90