<!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" οnclick="Compare();">输入日期与当前日期比较</button> </body> </html>