get the list of years onwards base from the given start year

How can I get the list of years onwards base from the given start year?

I tried

var start_year = "2011",d = new Date( "01 " + start_year),first = d.getFullYear();
console.log(first);

the expected output should be,

2011,2012,2013,2014,2015,2016

where 2016 is the latest date.

but seems not working at all, any ideas, help please?

Answers 1

  • var start_year = 2011;//If you receive as a string , let it be a string
    var current_year = new Date().getFullYear();
    for (var i = start_year; i <= current_year; i++) {
        console.log(i);
    }
    

    In your code, d = new Date( "01 " + start_year) it will say Invalid Date

    For Better answers, check comment from Rayon and Idea from dandavis.


Related Articles