不要判断了,直接让他选不到非法的日期就行了:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
</head>
<body onload=func()>
<select onchange=func() id=year>
<option value=1999>1999</option>
<option value=2000>2000</option>
<option value=2001>2001</option>
<option value=2002 selected>2002</option>
<option value=2003>2003</option>
<option value=2004>2004</option>
<option value=2005>2005</option>
</select>
<select onchange=func() id=month>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
<option value=5>5</option>
<option value=6>6</option>
<option value=7>7</option>
<option value=8>8</option>
<option value=9>9</option>
<option value=10 selected>10</option>
<option value=11>11</option>
<option value=12>12</option>
</select>
<select id=day></select>
<button onclick=alert(year.value+"年"+month.value+"月"+day.value+"日")>生成日期</button>
<SCRIPT LANGUAGE="JavaScript">
<!--
function func()
{
with (day)
while (children.length>0)
removeChild(lastChild)
var days=0;
with (month)
if (value == 2)
{
if (year.value % 4 != 0) days=28
else if (year.value % 100 != 0) days = 29
else if (year.value % 400 != 0) days = 28
else days=29
}
else if (value==1 || value==3 || value==5 || value==7 || value==8 || value==10 || value==12)
days=31
else days=30
for (var i=1;i<days+1;i++)
{
var op = new Option();
op.value=i
op.innerText=i
day.insertBefore(op)
}
}
//-->
</SCRIPT>
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
</head>
<body onload=func()>
<select onchange=func() id=year>
<option value=1999>1999</option>
<option value=2000>2000</option>
<option value=2001>2001</option>
<option value=2002 selected>2002</option>
<option value=2003>2003</option>
<option value=2004>2004</option>
<option value=2005>2005</option>
</select>
<select onchange=func() id=month>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
<option value=5>5</option>
<option value=6>6</option>
<option value=7>7</option>
<option value=8>8</option>
<option value=9>9</option>
<option value=10 selected>10</option>
<option value=11>11</option>
<option value=12>12</option>
</select>
<select id=day></select>
<button onclick=alert(year.value+"年"+month.value+"月"+day.value+"日")>生成日期</button>
<SCRIPT LANGUAGE="JavaScript">
<!--
function func()
{
with (day)
while (children.length>0)
removeChild(lastChild)
var days=0;
with (month)
if (value == 2)
{
if (year.value % 4 != 0) days=28
else if (year.value % 100 != 0) days = 29
else if (year.value % 400 != 0) days = 28
else days=29
}
else if (value==1 || value==3 || value==5 || value==7 || value==8 || value==10 || value==12)
days=31
else days=30
for (var i=1;i<days+1;i++)
{
var op = new Option();
op.value=i
op.innerText=i
day.insertBefore(op)
}
}
//-->
</SCRIPT>
</body>
</html>
<select id=year onchange=toDate()>
<script>for(i=1970;i<=2010;i++)document.write("<option>"+i+"</option>")</script>
</select>
<select id=month onchange=toDate()>
<script>for(i=1;i<=12;i++)document.write("<option>"+i+"</option>")</script>
</select>
<select id=day onchange=toDay()></select>
<input name=weekday>
</form>
<script>
var arr="日一二三四五六".split("")function toDate(){
with(document.all){
vYear=parseInt(year.options[year.selectedIndex].text)
vMonth=parseInt(month.options[month.selectedIndex].text)
day.length=0;
for(i=0;i<(new Date(vYear,vMonth,0)).getDate();i++){day.options[day.length++].value=day.length;day.options[day.length-1].text=day.length;}
}
toDay();
}function toDay(){
vDay=parseInt(document.all.day.options[document.all.day.selectedIndex].value)
document.all("weekday").value="星期"+arr[new Date(vYear,vMonth-1,vDay).getDay()]
}window.onload=toDate;
</script>
是日期:true
不是日期:false
<script>alert(new Date(2002,2,0).getDate())</script>http://www.blueidea.com/bbs/newsdetail.asp?id=472344<script>
function strDate(str){
var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/;
var r = str.match(reg);
if(r==null)return false;
var d= new Date(r[1], r[3]-1,r[4]);
var newStr=d.getFullYear()+r[2]+(d.getMonth()+1)+r[2]+d.getDate()
return newStr==str
}
alert(strDate("2002-1-31"))
alert(strDate("2002-2-31"))
alert(strDate("2002-1-41"))
</script>
但是我知道2002年3月1日减去一天一定是2002年2月的末天
---------------------------------------------------------------------------------秋水正是这么干的,不止2月这么干,每个月都是这么干的。---------------------------------------------------------------------------------
for(i=0;i<(new Date(vYear,vMonth,0)).getDate();i++){....
---------------------------------------------------------------------------------注意“(new Date(vYear,vMonth,0)).getDate()”
查询的时候的日期选择
function test(){
var Temp=new Date('2002/2/29');
var TempMon=Temp.getMonth();
if(parseInt(TempMon)==(1)){
return true;
}
return false;
}
function getDigit(str,type){
var dates;
var k=0;
if(type==1)
{
dates = new Array("","","","","","");
} else {
dates = new Array("","","");
}
for (i=0;i<dates.length;i++){
for (j=k;j<str.length;j++){
if (str.charAt(j)==' '||isNaN(str.charAt(j))) continue;
else break;
}
k=j;
for (j=k;j<str.length;j++){
//if (str.charAt(j)==' ') continue; //去除前面的空格
if (parseInt(str.charAt(j))>=0){
dates[i] = dates[i]+str.charAt(j);
}else break;
}
k=j+1;
}
/*
if (dates[0].length==2&&parseInt(dates[0])>80){
dates[0] = "19"+dates[0];
}else if (dates[0].length==2&&parseInt(dates[0])<81){
dates[0]= "20"+dates[0];
}*/
if (type == 1)
return dates[0]+"-"+dates[1]+"-"+dates[2]+"-"+dates[3]+"-"+dates[4]+"-"+dates[5];
else
return dates[0]+"-"+dates[1]+"-"+dates[2];
}function reWriteDate(time,type){
var tradetime_to = document.getElementById(time).value; if(type==3){
if(tradetime_to.length>0 && tradetime_to.length<=11)
type=2;
else
type=1;
}
tradetime_to = getDigit(tradetime_to,type);
tradetime_to = tradetime_to.split("-");
//year
if (tradetime_to[0].length!=4){
//alert("年份必须为四位数字!");
tradetime_to[0] = new Date().getFullYear();
}else if (parseInt(tradetime_to[0])<1850){
// alert("年份超出范围!");
tradetime_to[0] = 1850;
}else if (parseInt(tradetime_to[0])>2030){
tradetime_to[0] = 2030;
}
//month
if (tradetime_to[1].length<=0 || tradetime_to[1].length>2){
//alert("月份必须是数字!");
tradetime_to[1] = (parseInt(new Date().getMonth())+1).toString();
}
if( tradetime_to[1] == '0'){
tradetime_to[1] = "01";
}
var tempmonth = parseInt(tradetime_to[1])-1;
if (parseInt(tradetime_to[1])==0 && tradetime_to[1].length==2){
tempmonth = parseInt(tradetime_to[1].substring(1,2))-1;
}
if (parseInt(tradetime_to[1])>0&&parseInt(tradetime_to[1])<10 && tradetime_to[1].length==1){
tradetime_to[1] = "0"+tradetime_to[1];
}else if (parseInt(tradetime_to[1])>12){
//alert("月份超出范围!");
tradetime_to[1] = "01";
tempmonth = 0;
}//day
if (tradetime_to[2].length<=0 || tradetime_to[2].length>2){
//alert("日 必须是数字!");
tradetime_to[2] = parseInt(new Date().getDate());
}
if( tradetime_to[2] == '0'){
tradetime_to[2] = "01";
}
var tempyear = parseInt(tradetime_to[0]);
if (parseInt(tradetime_to[2])>0&&parseInt(tradetime_to[2])<10 && tradetime_to[2].length==1){
tradetime_to[2] = "0"+tradetime_to[2];
} else if (parseInt(tradetime_to[2])>daysInMonth[tempmonth]){
if ((((0 == tempyear % 4) && (0 != (tempyear % 100))) ||(0 == tempyear % 400)) &&
tradetime_to[1] == "02" && parseInt(tradetime_to[2])== 29)
{
tradetime_to[2] = "29";
}else
//alert("日 超出范围!");
tradetime_to[2] = "01";
}
if (type==1)
{
//hour
if (tradetime_to[3].length<=0 || tradetime_to[3].length>2){
// alert("小时必须为数字!");
tradetime_to[3] = (new Date().getHours()).toString();
}
if (parseInt(tradetime_to[3])>0&&parseInt(tradetime_to[3])<10 && tradetime_to[3].length==1){
tradetime_to[3] = "0"+tradetime_to[3];
}else if (parseInt(tradetime_to[3])>23){
// alert("小时超出范围!");
tradetime_to[3] = "00";
}//minute
if (tradetime_to[4].length<=0 || tradetime_to[4].length>2){
// alert("分钟必须是数字!");
tradetime_to[4] = (new Date().getMinutes()).toString();
}
if (parseInt(tradetime_to[4])>0&&parseInt(tradetime_to[4])<10 && tradetime_to[4].length==1){
tradetime_to[4] = "0"+tradetime_to[4];
}else if (parseInt(tradetime_to[4])>59){
// alert("分钟超出范围!");
tradetime_to[4] = "00";
} //second
tradetime_to[5] = "00";
}
document.getElementById(time).value = tradetime_to[0]+"-"+tradetime_to[1]+"-"+tradetime_to[2];
if(type == 1)
document.getElementById(time).value += " "+tradetime_to[3]+":"+tradetime_to[4]+":"+tradetime_to[5];
}function getType(id){
var str=document.all[id].value;
if(str.length && str.length<=11)
return 2;
else
return 1;}
在javascript论坛中向高手学了很多,现在我也希望能帮助需要帮助的人!
<!--
''************************************************
'' Copyright (C), 2002, launch Tech
'' Project name: Inside Office--Sale System
'' @Author: qiu liang
'' @Version: V2.0, 2002/10/9
'' @Update Date: 2002/10/10
'' Description: common functionfile
'' Other:
''************************************************
//-->
<script language="javascript">
<!--
// 检查日期格式的有效性
function ckdateformat(inpar)
{
var reg;
reg=/^(\d{4})-(\d{1,2})-(\d{1,2})$/;
var r = inpar.match(reg);
if(r==null)
{
return false; // 日期格式检验未通过
}
else
{
return true; // 日期格式检验通过
}
} // 检查日期值的有效性
function ckdateValue(inpar)
{
var inparDate;
inparDate=inpar.split("-");
inparY=inparDate[0]; // 得到年份
inparY=parseInt(inparY);
inparM=inparDate[1]; // 得到月份
inparM=parseInt(inparM);
inparD=inparDate[2]; // 得到日期
inparD=parseInt(inparD);
switch (inparM){
case 2: // 若为二月需要判断是否为闰年
{
if ((inparY%400==0)||((inparY%4==0)&&(inparY%100!=0)))
{
if ((inparD>0)&&(inparD<29)) return true;
}
else if ((inparD>0)&&(inparD<28)) return true;
else
{
alert("输入的日期不正确!");
return false;
}
break;
}
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
{
if ((inparD>0) && (inparD<32)) return true;
else
{
alert("输入的日期不正确!");
return false;
}
break;
}
case 4:
case 6:
case 9:
case 11:
{
if ((inparD>0) && (inparD<31)) return true;
else
{
alert("输入的日期不正确!");
return false;
}
break;
}
default:
{
alert("输入的月份不正确!");
return false;
}
}
}
// 检查日期有效性的函数
function ckDate(tempDate)
{
if (ckdateformat(tempDate))
{
return ckdateValue(tempDate);
}
else
{
alert("日期格式不正确!");
}
} //-->
</script>