下拉框显示当天日期
分类:电脑知识 发表于:2010-12-19 09:49:10 评论(1)
下拉框显示当天日期代码:
<SCRIPT>
now = new Date();
function writeYears(obj, date)
{
var years = date.getYear()+100
for (var i=2010; i<2015; i++)
{
var opt = document.createElement("option")
opt.text = opt.value = i
obj.add(opt)
}
}
function writeMonths(obj)
{
for (var i=1; i<13; i++)
{
var opt = document.createElement("option")
opt.text = opt.value = Math.floor(i/10) ? i : "0"+i
obj.add(opt)
}
}
function writeDate(obj, year, month)
{
var date = new Date(""+year+"/"+(parseInt(month)+1)+"/"+0).getDate()
var selectedIndex = (date-1)<obj.selectedIndex ? (date-1) : obj.selectedIndex
var len = obj.options.length
if (date>len)
for (var i=len+1; i<=date; i++)
{
var opt = document.createElement("option")
opt.text = opt.value = Math.floor(i/10) ? i : "0"+i
obj.add(opt)
}
else
for (var i=date; i<len; i++)
{
obj.remove(obj.length-1)
}
obj.selectedIndex = selectedIndex
}
function changeDate()
{
var c_year = eval("document.forms[0].c_year")
var c_month = eval("document.forms[0].c_month")
var c_day = eval("document.forms[0].c_day")
writeDate(c_day, c_year.options(c_year.selectedIndex).value,
c_month.options(c_month.selectedIndex).value)
}
</SCRIPT>
<FORM><SELECT onchange=changeDate() name=c_year></SELECT>年<SELECT
onchange=changeDate() name=c_month></SELECT>月<SELECT
name=c_day></SELECT>日</FORM>
<SCRIPT language=javascript type=text/javascript>
writeYears(document.forms[0].c_year, now)
document.forms[0].c_year.value = now.getYear()
writeMonths(document.forms[0].c_month)
this_month = now.getMonth()+1
if (this_month<10){this_month = "0"+this_month}
document.forms[0].c_month.value = this_month
writeDate(document.forms[0].c_day, now.getYear(), now.getMonth()+1)
nowday = now.getDate()
if (nowday<10){nowday="0"+(now.getDate())*1}
document.forms[0].c_day.value = nowday
</SCRIPT>