Reputation: 423
I am trying to generate the list of years from 1901 to 2020 as a current year, current year can change but 1901 should be same
below is the logic i am trying but there i have to mention specific number, if the current year changes to 2025 then start year will also get changes.
Below is logic i am trying to use it to extract years which is not correct for my logic
const currentYear = 2030;
const range = (start, stop, step) => Array.from({
length: (stop - start) / step + 1
}, (_, i) => start + (i * step));
console.log(range(currentYear, currentYear - 120, -1));
please help me with this to get years
Upvotes: 0
Views: 505
Reputation: 141
getList = () => {
const currentYear = new Date().getFullYear();
const resultArr = [];
let year = 1901;
while (year <= currentYear) {
resultArr.push(year);
++year;
}
return resultArr;
}
Upvotes: 1
Reputation: 30715
I think with a small change, this will give you the right result:
const currentYear = 2030;
const range = (start, stop, step) => Array.from({
length: (stop - start) / step + 1
}, (_, i) => start + (i * step));
console.log("Years:",range(currentYear, 1901, -1));
Upvotes: 1