需要增加的地方:
2010
2011
2012
2013
测试代码:
* 您消费的时间* 您消费的时间 | 2007 2008 2009 2010 2011 2012 2013 年 | 01 02 03 04 05 06 07 08 09 10 11 12 月 | 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 日 | 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 点 | 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 分 |
var today=new Date()//定义一个时间对象
var yy=today.getYear()
var mm=today.getMonth()+1
var dd=today.getDate()
var h=today.getHours()//定义小时
var m=today.getMinutes()//定义分钟
document.getElementById("years").options(yy-2007).selected=1
document.getElementById("months").options(mm-1).selected=1
document.getElementById("days").options(dd-1).selected=1
document.getElementById("hours").options(h).selected=1
document.getElementById("mins").options(m).selected=1
第二种方法:不需要事先写好年份,可扩展性比较好
New Documentfunction setDay(obj){
obj = obj.form;
var years=parseInt(obj.years.options[obj.years.selectedIndex].value);
var months=parseInt(obj.months.options[obj.months.selectedIndex].value);
if(obj.years.selectedIndex==0 || obj.months.selectedIndex==0)return;
var lastday = monthday(years,months);
var itemnum = obj.days.length;
if (lastday - 1 < obj.days.selectedIndex)
{
obj.days.selectedIndex = lastday - 1;
}
obj.days.length = lastday;
for(cnt = itemnum + 1;cnt <= lastday;cnt++)
{
obj.days.options[cnt - 1].text = cnt;
}
}
function monthday(years,months)
{
var lastday = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
if (((years % 4 == 0) && (years % 100 != 0)) || (years % 400 == 0))
{
lastday[1] = 29;
}
return lastday[months - 1];
}
function forto(ff,to)
{
document.write('');
for(var ii=ff; ii<=to; ii++)
document.write(''+ii+'');
}
function a()
{
alert(document.all("years").value+"年"+document.all("months").value+"月"+document.all("days").value+"日") ;
}
年
月
日
本文介绍了一种使用JavaScript实现动态日期选择器的方法。该方法能够自动调整年份、月份和日期选项,并考虑到不同月份的天数差异及闰年情况。通过两个不同的实现方案,展示了如何使日期选择器更具灵活性和可扩展性。

1201

被折叠的 条评论
为什么被折叠?



