您的位置:首页 > Web前端 > JavaScript

JavaScript日期比较

2009-04-27 15:10 148 查看
<!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>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="" />
<meta name="keywords" content="" />
<meta name="description" content="" />

<mce:script type="text/javascript"><!--
function CompareDate(inputDate){
var currentDate = new Date();
if(currentDate.getYear() > inputDate.getYear())
return -1;
else if(currentDate.getYear() < inputDate.getYear())
return 1;
if(currentDate.getMonth() > inputDate.getMonth())
return -1;
else if(currentDate.getMonth() < inputDate.getMonth())
return 1;
if(currentDate.getDay() > inputDate.getDay())
return -1;
else if(currentDate.getDay() < inputDate.getDay())
return 1;
return 0;
}
function Compare(){
var inputDate = new Date(document.getElementById("txtDate").value);
if(isNaN(inputDate)){
alert("不是日期");
return;
}
var result = CompareDate(inputDate);
if(result < 0)
alert(inputDate.toLocaleString() + "小于当前日期");
else if(result > 0)
alert(inputDate.toLocaleString() + "大于当前日期");
else
alert(inputDate.toLocaleString() + "等于当前日期");
}

// --></mce:script>
</head>
<body>
<input type="text" id="txtDate" value="5-1-2009"></input><span style="color:red" mce_style="color:red">格式:月-日-年</span>
<button type="button" onclick="Compare();">输入日期与当前日期比较</button>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: