欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JS获取指定月份到当前月份列表,并生成下拉列表框

程序员文章站 2022-03-27 12:37:58
...
JS获取指定月份到当前月份列表,并生成下拉列表框

function getMonthList() {
var statisMonth = document.forms[0].statis_date;
var date = new Date();
var startYear = 2010;
var startMonth = 1;
var curYear = date.getYear();
var curMonth = date.getMonth() + 1;
if (curMonth == 1) {
curYear--;
curMonth = 12;
} else {
curMonth--;
}
var list = new Array();
for (var i = startYear; i <= curYear; i++) {
for (var j = 1; j <= 12; j++) {
if (((i == startYear && j < startMonth) || (i == curYear && j > curMonth))) {
} else {
var monthStr = "";
if (j < 10){
monthStr = "0" + j;
}else{
monthStr = j;
}
var yearMonth = i +"-"+ monthStr;
list[(i-startYear+1)*j-1] = yearMonth;
}
}
}
list = list.reverse();
for(var i=0;i<list.length;i++){
var length = statisMonth.options.length
statisMonth.options.length++;
statisMonth.options[i].value = list[i];
statisMonth.options[i].text = list[i];
if(list[i] == "<c:out value="${statis_date}" />"){
statisMonth.options[i].selected = true;
}
}
}
相关标签: J#