你能解释一下为什么这个javascript不正确吗

Can you explain why this javascript is incorrect?

本文关键字:javascript 不正确 为什么 一下 能解释      更新时间:2023-09-26

如果过期日期在过去,我希望此脚本返回警报,否则显示OK。
我没想到脚本会落入Else"Error"语句中。

有人能解释一下发生了什么吗?

<!DOCTYPE HTML>
<html>
<body>
<div id="test"></div>
  <script>
    var expirymonth = "3";
    var expiryyear = "2017";
    if (expirymonth != null != null && expiryyear != null)
    {
        var currentDate = Date();
        var expiryDate = new Date(parseInt(expiryyear),parseInt(expirymonth - 1),1);
        if (expiryDate < currentDate)
        {
            window.alert("Expiry Date must not be in the past.");
        }
        else if (expiryDate > currentDate)
        {
            window.alert("OK");
        }
        else
        {
            window.alert("Error");
        }
    }
  </script>
</body>
</html>

Date()返回一个字符串。你不能把它比作约会。

更换

var currentDate = Date();

带有

var currentDate = new Date();

(还修复了Davin Tryon在评论中指出的明显打字错误)