javascript日期比较

来源:互联网 发布:淘宝店铺优惠如何设置 编辑:程序博客网 时间:2024/06/07 06:43

 如:比较"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>
原创粉丝点击