Hello Friends, Today I will Tell you how we can add month and year in dropdown using javascript.

 

How to show year range (2010-2025) in dropdown using for loop in javascript:

1- Create a layout of dropdown list using select and option tag in html.

2- Give the name and id of the select tag.

name=”year” id=”year

3- Write this code in script opening and closing tag:

for(y = 2000; y <= 2025; y++) {
var optn = document.createElement(“OPTION”);
optn.text = y;
optn.value = y;

// if year is 2015 selected
if (y == 2015) {
optn.selected = true;
}

document.getElementById(‘year’).options.add(optn);
}

How to show month in dropdown using for loop in javascript:

1- Create a layout of dropdown list using select and option tag in html.
2-Enter the name and id of select tag.

name=”month” id=”month”

3-Write this code in between script opening and closing tag:

var d = new Date();
var monthArray = new Array();
monthArray[0] = “January”;
monthArray[1] = “February”;
monthArray[2] = “March”;
monthArray[3] = “April”;
monthArray[4] = “May”;
monthArray[5] = “June”;
monthArray[6] = “July”;
monthArray[7] = “August”;
monthArray[8] = “September”;
monthArray[9] = “October”;
monthArray[10] = “November”;
monthArray[11] = “December”;
for(m = 0; m <= 11; m++) {
var optn = document.createElement(“OPTION”);
optn.text = monthArray[m];
// server side month start from one
optn.value = (m+1);

// if june selected
if ( m == 5) {
optn.selected = true;
}

document.getElementById(‘month’).options.add(optn);
}