'How to get the current month and the next months until next year in javascript

so I want to get the current month and the next 12 months in vanilla js. For example, upon writing this question it's April 2022, So I want to have this output:

April 2022 May 2022 June 2022 July 2022 August 2022 September 2022 October 2022 November 2022 December 2022 January 2023 February 2023 March 2023 April 2023



Solution 1:[1]

for (let i = 0; i < 13; i++) {
  let month = new Date().getMonth() + 1 + i;
  let year = new Date().getFullYear();
  if (month > 12) {
    month = month - 12;
    year = year + 1;
  }
  const nameMonth = {
    1: "Jan",
    2: "Feb",
    3: "Mar",
    4: "Apr",
    5: "May",
    6: "Jun",
    7: "Jul",
    8: "Aug",
    9: "Sep",
    10: "Oct",
    11: "Nov",
    12: "Dec",
  };
  console.log(year + " Year " + nameMonth[month] + " Month ");
}

Solution 2:[2]

for (let i = 0; i < 13; i++) {
  let month = new Date().getMonth() + 1 + i;
  let year = new Date().getFullYear();
  if (month > 12) {
    month = month - 12;
    year = year + 1;
  }
  const nameMonth = {
    1: "Jan",
    2: "Feb",
    3: "Mar",
    4: "Apr",
    5: "May",
    6: "Jun",
    7: "Jul",
    8: "Aug",
    9: "Sep",
    10: "Oct",
    11: "Nov",
    12: "Dec",
  };
  console.log(
    year + " Year " + nameMonth[month] + " Month "
  );
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 chebread
Solution 2