[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 649
  • Last Modified:

date.getYear in Firefox 2.0.0.8 and IE7/Opera

My Function:

function changeyear(val) {

      var element = document.getElementById("year");

      var date = new Date();
      var thisyear = date.getYear();
      thisyear = (thisyear + 1900);

      // BMW New MINI
      if (val == "New Mini (MINI)")
      {
            // remove all the <option> current in the drop down
            while (element.firstChild) {
                  element.removeChild(element.firstChild);
            }

            // Add in 'Any' to top of list
            element.options[element.options.length] = new Option("Any", "Any");

            // MINI - 2001 to Present Day
            for(var y = parseInt(thisyear); y >= 2001; y--)
            {
                  element.options[element.options.length] = new Option(y, y);
            }  
        
      } else {

            // remove all the <option> current in the drop down
            while (element.firstChild) {
                  element.removeChild(element.firstChild);
            }

            // Add in 'Any' to top of list
            element.options[element.options.length] = new Option("Any", "Any");

            // Classic Mini - 1959 to Present Day (for new registrations)
            for(var y = parseInt(thisyear); y >= 1959; y--)
            {
                  element.options[element.options.length] = new Option(y, y);
            }
      }
}

When "Classic Mini" is selected in one drop down it should change the other one to go from 1959 -> 2007. When "New Mini" is selected it should change the other drop down to 2001 -> 2007.

I commented that line out, and tried it in Firefox and it does not populate the drop downs at all.

so,
Code:

      var date = new Date();
      var thisyear = date.getYear();
      //thisyear = (thisyear + 1900);

works in MS IE 7 and Opera 9.23 - but not Firefox 2.0.0.8.

and
Code:

      var date = new Date();
      var thisyear = date.getYear();
      thisyear = (thisyear + 1900);

works in Firefox 2.0.0.8, but not IE7/Opera 9.23.

I could put a piece of code in there to check for the browser, but it's a bit of a hack! :-( Any way to amend the code ...... properly?
Picco
0
crmpicco
Asked:
crmpicco
1 Solution
 
NeoTeqCommented:
The best thing to do is use date.getFullYear(), this will always return a 4-digit year.
0
 
crmpiccoAuthor Commented:
excellent, that is exactly what was needed. posted my code up for others:

function changeyear(val) {

      var element = document.getElementById("year");

      var date = new Date();
      var thisyear = date.getFullYear();
      //thisyear = (thisyear + 1900);

      // BMW New MINI
      if (val == "New Mini (MINI)")
      {
            // remove all the <option> current in the drop down
            while (element.firstChild) {
                  element.removeChild(element.firstChild);
            }

            // Add in 'Any' to top of list
            element.options[element.options.length] = new Option("Any", "Any");

            // MINI - 2001 to Present Day
            for(var y = parseInt(thisyear); y >= 2001; y--)
            {
                  element.options[element.options.length] = new Option(y, y);
            }  
        
      } else {

            // remove all the <option> current in the drop down
            while (element.firstChild) {
                  element.removeChild(element.firstChild);
            }

            // Add in 'Any' to top of list
            element.options[element.options.length] = new Option("Any", "Any");

            // Classic Mini - 1959 to Present Day (for new registrations)
            for(var y = parseInt(thisyear); y >= 1959; y--)
            {
                  element.options[element.options.length] = new Option(y, y);
            }
      }
}
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now