0

I'm not sure how to ask this, but is there a way to get the current local full year and then create a loop to get the last 18 years and print them to the screen?

Here is what I've tried...

function allYears(){
  var d = new Date();
  var y = d.getFullYear();
  var years = '';
  for(var i=0; i>18; i++){
    years += '<option value="'+y+'">'+y+'</option>';
    y = y-1;
  }
return(years);
}
3
  • 1
    Note d.getFullYear should be d.getFullYear() Commented Jul 24, 2012 at 20:20
  • 3
    The body of your loop is never executed since i = 0 and 0 > 18 is false. Commented Jul 24, 2012 at 20:26
  • +1 Felix Kling, your comment was very helpful! I set up some alerts but the for loop would never execute and I couldn't figure out why. Thank you! Commented Jul 24, 2012 at 21:28

4 Answers 4

2

You're close, but two problems:

1) d.getFullYear should be d.getFullYear(), otherwise you're not getting the year, merely referencing the method

2) > 18 should be < 18

Sign up to request clarification or add additional context in comments.

Comments

1

Fiddle with working example: http://jsfiddle.net/aMFpw/6/

You were very close with your original implementaion.

Javascript here:

  function getYears(num){
    var d  = new Date();
    var y = parseInt(d.getFullYear());

    var years = "";

    //need to add one to our number based on our loop index.
    num++;

    for(var i=0; i<num; i++){
        years += '<option value="' + y + '">' + y + '</option>';
        y--;    
    }

    return years;
 }

Comments

0

As a function:

function printYearsBefore(date,num) {
   var yr = parseInt(date.getFullYear()), i =0;
   while(++i <= num){
     document.write((yr-i));
   }
}

printYearsBefore(new Date(),18);

1 Comment

This only produces 17 previous years. You should change the while condition to ++i <= num.
0

I'd suggest performing math on the variable that refers to the year, as you increment (or decrement) through the for loop, simply add, or remove, the incrementing variable (i in this case) to, or from, the variable holding the year:

function addYears(el, num) {
    if (!el) {
        return false;
    }
    else {
        for (var i=0; i < num; i++) {
            var opt = document.createElement('option'),
                year = new Date().getFullYear() - i;
            opt.value = year;
            opt.textContent = year;
            el.appendChild(opt);
        }
    }
}

addYears(document.getElementById('years'), 18);​

JS Fiddle proof of concept.

In your code excerpt, I'd suggest amending things to:

function allYears(){
  var d = new Date();
  var y = d.getFullYear();
  var years = '';
  for(var i=0; i<18; i++){
    years += '<option value="'+ (y - i) +'">'+ (y - i) +'</option>';
  }
return(years);
}

Which, as the loop progresses, should return option elements with year values progressively earlier than the year held in y.

Also, the middle condition in the for loop is the evaluation that has to be true for the loop to continue (when it stops being true is when the loop exits), so it should be i < 18 (since i starts equal to 0).

1 Comment

This code only produces the last 17 years, not 18 like the answer specifies.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.