1

Hi guys is there any way in javascript that i can get the current full year and the next 5 years in the futue?

example:

new Date().getFullYear() and 'next 5 years in the future'

output:

[
  '2021',
  '2022',
  '2023',
  '2024',
  '2025',
  '2023'
]

Is there any way to do that? Appreciate the help guys :)

Diego Braga
  • 161
  • 11
  • seems like a simple loop... – epascarello Jul 22 '21 at 14:36
  • Do you want to loop through all dates in the array, add 5 years then generate an array of the same length? – its_tayo Jul 22 '21 at 14:37
  • Add the values 1 through 5 to the current year? – Pointy Jul 22 '21 at 14:37
  • https://stackoverflow.com/questions/1575271/range-of-years-in-javascript-for-a-select-box/1575326 – Robin Webb Jul 22 '21 at 14:40
  • 1
    Does this answer your question? [Create array of all integers between two numbers, inclusive, in Javascript/jQuery](https://stackoverflow.com/questions/8069315/create-array-of-all-integers-between-two-numbers-inclusive-in-javascript-jquer) – Peter B Jul 22 '21 at 14:53

1 Answers1

2

Unless I'm overlooking something, this is a simple as using a loop to add the next 5 years to an array.

The following snippet shows a simple example of this using a for loop:

let years = [];
let year = new Date().getFullYear();

for (let i = 0; i < 6; i++)
  years.push(year++);
  
console.log(years);

This works by creating an empty array names years, and then storing the current year in year. The for loop then adds each year from now to the next 5 years (i < 6) to the years array, incrementing year by one each time (year++).

Output:

[
  2021,
  2022,
  2023,
  2024,
  2025,
  2026
]
jsejcksn
  • 27,667
  • 4
  • 38
  • 62
Martin
  • 16,093
  • 1
  • 29
  • 48