var year = date.getFullYear() // 年份
var month = date.getMonth() + 1 // 月份(从0开始,所以应+1)
for(var i = month i > month - 6 i--) {
$("<option value=''>" + year + "年" + i + "月" + "</option>").appendTo($("select")) // 依次添加下拉框选项,value自定义,右边的appendTo参数为目标选择框
}
<BODY>
<select id="sel"></select>
</BODY>
<SCRIPT LANGUAGE="JavaScript">
window.onload = function()
{
var sel = document.createElement('select')
var date = new Date()
for(var i=0i<5i++)
{
var opt = document.createElement("option")
opt.value = i
opt.innerHTML = (date.getYear()-i-1)+"-"+(date.getYear()-i)
sel.appendChild(opt)
}
document.body.appendChild(sel)
}
</SCRIPT>
JS有自带的日期函数,获取如下:var date = new Date()var year = date.getFullYear()//获取年份var month = date.getMonth()+1//获取月份,因为是从0-11所以要+1var day = date.getDate()//获取天数var hour = date.getHours()//获取小时var minute = date.getMinutes()//获取分钟var second = date.getSeconds()//获取秒