Problems with javascript date

6

I have the following code:

var max = new Date().getFullYear(),
    min = max - 90,
    select = document.getElementById('selectYear');

for (var i = min; i<=max; i++){
    var opt = document.createElement('option');
    opt.value = i;
    opt.innerHTML = i;
    select.appendChild(opt);
}
<select id="selectYear">
</select>

I use it to popule me in a select the years, from the current year to 90 years less than this.

What I would like could help me is how to achieve the same result, but instead of presenting me the years from (1927 to 2017) I present them (2017 to 1927)

Some Suggestion

Thank you very much.

    
asked by Alexander Quiroz 11.04.2017 в 05:09
source

2 answers

4

It's just a matter of changing the for to start at the value Max until you reach the Value Mínimo , plus go Decrementando i instead of Incrementar

var max = new Date().getFullYear(),
    min = max - 90,
    select = document.getElementById('selectYear');

for (var i = max; i>=min; i--){
    var opt = document.createElement('option');
    opt.value = i;
    opt.innerHTML = i;
    select.appendChild(opt);
}
<select name="" id="selectYear">
    
answered by 11.04.2017 / 05:15
source
3

Just modify the for , doing something like this:

var max = new Date().getFullYear(),
    min = max - 90,
    select = document.getElementById('selectYear');

for (var i = max; i >= min; i--){
    var opt = document.createElement('option');
    opt.value = i;
    opt.innerHTML = i;
    select.appendChild(opt);
}
<select id="selectYear">
</select>

What we are indicating is that instead of running in an Ascendant way, it is executed in a descending way.

for (var i = max; i >= min; i--)

Run the loop from the largest element to the smallest element.

    
answered by 11.04.2017 в 05:15