js脚本: 在文本框中输入一个日期值后按确定按钮后和当前时间比较大小

如题所述

function check() {
var d = new Date();
//取当前年月日,舍去时分秒
d = new Date(d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate());
var d2 = new Date(document.getElementById("txtDate").value);
if (d2 == "Invalid Date") {
alert("非日期");
return;
}
//getTime 从1970.1.1开始的毫秒数
var n = d.getTime() - d2.getTime();
if (n == 0) {
alert("相等");
} else if (n > 0) {
alert("小于当前日期");
} else {
alert("大于当前日期");
}
}
<input id="txtDate" type="text" value="2013-1-17"/>
<input type="button" value="比较" onclick="check()"/>
温馨提示:答案为网友推荐,仅供参考
相似回答