HTML使用Javascript比较2个日期 [英] HTML Comparing 2 Dates with Javascript

查看:308
本文介绍了HTML使用Javascript比较2个日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Javascript比较2个日期。如果myDateL在mydateR之后,则在单击按钮时显示一个消息框。

I'm trying to compare 2 dates using Javascript. If "myDateL" is after "mydateR" display a message box when the button is clicked.

我的代码有什么问题?

我知道我见过类似的线索,但我无法理解。我希望有人可以帮我这个。

I know I've seen a similar thread to this but I couldn't understand it. I hope someone can help me with this please.

<input type="Button" value="TwoDates" onClick="twoDates()">

<script>

     function twoDates() {

        var firstdate = new date(document.getElementById("mydateL").value);
        var seconddate = new date(document.getElementById("mydateR").value);

     if(firstdate > seconddate) {

          alert('Please change your return date.');
            }
}
</script>


推荐答案

这是新日期(。 ..),而不是新日期(...)日期是保存日期和时间的全局对象, date 将是您声明的函数日期。如果你在运行它时查看控制台,你会看到如下内容:

It's new Date(...), not new date(...). Date is the global object that holds dates and times, date would be a function you've declared called date. If you look at the console when you run this, you should see something like:

ReferenceError: date is not defined

这篇关于HTML使用Javascript比较2个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆