我想创建一个包含按降序排列的日期的下拉列表。例如,从当前月份开始,格式为“6 2012”,一直到“3 2011”。
我有一个jsfiddle,这是代码:
var TheStartDate = new Date(2011, 4, 2);
var TheCurrentDate = new Date();
TheCurrentDate = new Date(TheCurrentDate.getUTCFullYear(),
TheCurrentDate.getMonth(), 2);
var TheHTML = '<select>';
do {
TheHTML = TheHTML + '<option>';
TheHTML = TheHTML + (TheCurrentDate.getUTCMonth() + 1).toString() + " ";
TheHTML = TheHTML + TheCurrentDate.getFullYear();
TheHTML = TheHTML + '</option>';
TheCurrentDate.setMonth(TheCurrentDate.getUTCMonth() - 1);
} while (TheStartDate.getTime() !== TheCurrentDate.getTime());
TheHTML = TheHTML + '</select>';
alert(TheHTML);
document.getElementById('TheTestDiv').innerHTML = TheHTML;
我不是太远,但我认为我错过了一些明显的东西。
感谢您的建议。