简体   繁体   English

为什么jQuery UI的datepicker中的这两个日期在jQuery中没有正确比较?

[英]Why don't these two dates from jQuery UI's datepicker compare correctly in jQuery?

I have been trying to work with comparing dates and times using jQuery UI's datepicker and the timepicker add-on (an add-on I have just discovered and am very happy with). 我一直在尝试使用jQuery UI的datepicker和timepicker add-on(我刚刚发现并且非常满意的附加组件)来比较日期和时间。

So, here's what I have: 所以,这就是我所拥有的:

First, the expected values from the $("#dateOfAb").val() and the $("#dateOfAb2").val() both use the datetimepicker with the following formatting: 首先, $("#dateOfAb").val()$("#dateOfAb2").val()的预期值都使用datetimepicker,格式如下:

Time: 时间:

$("#dateOfAb, #dateOfAb2").datetimepicker({ timeFormat: "hh:mm tt", stepMinute: 15 }); $(“#dateOfAb,#dateOfAb2”)。datetimepicker({timeFormat:“hh:mm tt”,stepMinute:15});

AND, although not explicitly set (It seems to be the default date formatting for said plug-in): AND,虽然没有明确设置(它似乎是所述插件的默认日期格式):

dateFormat: "mm/dd/yy" dateFormat:“mm / dd / yy”

(example output: 08/16/2013 12:00 am) (示例输出:08/16/2013 12:00 am)

So, I need to compare these dates and while I have had success comparing dates in the past, I am having trouble comparing times for equal dates (which is the ultimate goal), but what is holding me up right now is the fact that I can't get these two parseDate values (on substrings from the full date/time) to show that they are equal when the dates are the same. 所以,我需要比较这些日期,虽然我已经成功地比较了过去的日期,但我在比较相等日期的时间(这是最终目标)时遇到了麻烦,但是现在阻碍我的是我无法获取这两个parseDate值(在完整日期/时间的子字符串上),以显示它们在日期相同时相等。

Okay, that last paragraph may be a little confusing so, here's the code: 好的,最后一段可能有点令人困惑,所以这里是代码:

$("#dateOfAb2").change(function () {
    console.log("FIRST SET: " + $.datepicker.parseDate("mm/dd/yy", $("#dateOfAb").val().substring(0, 9)))
    console.log("FIRST SET: " + $.datepicker.parseDate("mm/dd/yy", $("#dateOfAb2").val().substring(0, 9)))

    if ($.datepicker.parseDate("mm/dd/yy", $("#dateOfAb").val().substring(0, 9)) == $.datepicker.parseDate("mm/dd/yy", $("#dateOfAb2").val().substring(0, 9))) {
        alert("The dates are tied.");
        console.log("SECOND SET: " + $.datepicker.parseDate("mm/dd/yy", $("#dateOfAb").val().substring(0, 9)))
        console.log("SECOND SET: " + $.datepicker.parseDate("mm/dd/yy", $("#dateOfAb2").val().substring(0, 9)))
    }
});

The problem is that even when I know the values are exactly the same (proven with console.log() [image below]) they don't seem to compare as I would expect in my if branch. 问题是,即使我知道值完全相同(使用console.log() [下面的图片]证明),它们似乎没有像我在if分支中所期望的那样进行比较。

Image of console after running: 运行后控制台的图像:

在此输入图像描述

You can see that I have substringed out just the date part of the value and the values are the same, but I'm not sure what I'm doing wrong and why the "SECOND SET:" of console.log() commands won't execute. 您可以看到我只是将值的日期部分包含在内并且值相同,但我不确定我做错了什么以及为什么console.log()命令的“SECOND SET:”赢了执行。

The parse routine returns Date objects, so your comparison is between two separate object instances. 解析例程返回Date对象,因此您的比较是在两个单独的对象实例之间进行的。 They'll never be == to each other. 他们永远不会彼此==

Try: 尝试:

if ($.datepicker.parseDate("mm/dd/yy", $("#dateOfAb").val().substring(0, 9)).getTime() == $.datepicker.parseDate("mm/dd/yy", $("#dateOfAb2").val().substring(0, 9)).getTime()) {

By comparing the results of calling .getTime() , you're comparing two numbers for equality, which will do what you want. 通过比较调用.getTime()的结果,您将比较两个数字的相等性,这将做你想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM