javascript日期比较

javascript日期比较

 如:比较"2007-9-9"与"2007-10-10"

方法:先转成符合javascript的date格式,然后进行比较

 

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="javascript">...
    function check()
    ...{
        try
        ...{
            var strdt1=document.getelementbyid("dt1").value.replace("-","/");
            var strdt2=document.getelementbyid("dt2").value.replace("-","/");            
            var dt1=new date(date.parse(strdt1));
            var dt2=new date(date.parse(strdt2));
            alert(dt1>dt2);
        }
        catch(e)
        ...{
            alert("格式错误"+e);
        }
    }
</script>
</head>
<body>
<form onsubmit="return check();">
<input name="dt1" id="dt1"  value="2007-9-9" />
<input name="dt2" id="dt2" value="2007-10-10"/>
<input type="submit" value="提交" />
</form>
</body>
</html>

你可能感兴趣的:(JavaScript,html,XHTML)