How can I generate a list of array till today? I can't hardcode [2016,2017,2018]
, because I have to do it every year.
function (numberOfYearsBack) { // }
Get the current year using getFullYear()
, and use Array.from()
with a map function to declare and initialize an array with the values you need:
const years = (back) => {
const year = new Date().getFullYear();
return Array.from({length: back}, (v, i) => year - back + i + 1);
}
console.log(years(3));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With