-2

Problem

First off, I am going to clarify that this question is not a duplicate of Difference in months between two dates in javascript or javascript month difference

My question is specifically about getting the months in between two dates, not the number of months.

Expected Results

So If date1 is 11/01/2022 (mm/dd/yyyy) and date2 is 02/20/2023, it should output an array of months including the month of date1 and date2 like ["November", "December", "January", "February"]. I need to know how to return the actual months between two dates, not the number of months. Can somebody explain what would be the way to do that?

XYBOX
  • 69
  • 7
  • Does this answer your question? [List of all the months that exists between the 2 dates including case when num difference between dates is less than 30 days](https://stackoverflow.com/questions/69182964/list-of-all-the-months-that-exists-between-the-2-dates-including-case-when-num-d) – Thomas Sablik Dec 03 '22 at 12:33
  • @ThomasSablik That question is specific to momentjs, my question is about just JavaScript. – XYBOX Dec 03 '22 at 12:35
  • The logic is exactly the same as in the accepted answer. The duplicate contains answers with momentjs and date-fns, but the same logic could be used with native `Date` objects. – Thomas Sablik Dec 03 '22 at 13:09

2 Answers2

1

The post linked in the question is already a good start.

Maybe think about how you would do it when you want to write the results to a sheet of paper.

When we know the month to start, as an index [0...11], we can count from there and add the month names from an array:

const xmas = new Date("December 25, 1999 23:15:30");
const summer = new Date("June 21, 2003 23:15:30");

function monthsBetween(dstart, dend) {
    const monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
    let result = [];
    let current = dstart.getMonth();
    let end = (dend.getFullYear() - dstart.getFullYear()) * 12 + dend.getMonth();
    for (;current <= end; current += 1) {
        result.push(monthNames[current % 12]);
    }
    return result;
}

console.log(monthsBetween(xmas, summer)); // [December, January, February..., December, January, ...., June (multiple years)
console.log(monthsBetween(xmas, xmas)); // ["December"]

In your example 2022-11-01 to 2023-02-20, current would count from 10 (November, indexed from 0) to 13 (1: February indexed from 0 + 1 year = 12 months difference)

thejonny
  • 488
  • 2
  • 9
  • That's a great answer, thanks a lot! Also, apologies for not including a reproducible example as I am not on my main computer with code but had to ask this question at the earliest, but you got it nonetheless! :D – XYBOX Dec 03 '22 at 12:29
1

You can loop over the dates from the start to the end, collecting the month names as you go.

The start and end should be Date objects, how you convert them from timestamps like "11/01/2022" is a separate concern and should be dealt with in a prior step.

/* Get month names from start date to end date
 * @param {Date} start - date to start from
 * @param {Date} end - date to end at
 * @param {string} lang - language for month names
 * @returns {string[]} month names from start to end inclusive
 */
function getMonthNames(start = new Date(), end = new Date(), lang = 'en') {
  let d = new Date(start.getFullYear(), start.getMonth());
  let months = [];
  while (d <= end) {
    months.push(d.toLocaleString(lang,{month:'long'}));
    d.setMonth(d.getMonth() + 1);
  }
  return months;
}

let start = new Date(2022, 10, 1);  // 01 Nov 2022
let end   = new Date(2023,  1, 20); // 20 Feb 2023 

console.log(getMonthNames(start, end))
RobG
  • 142,382
  • 31
  • 172
  • 209