使用 JavaScript 比较两个日期没有按预期工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/4790828/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Comparing two dates using JavaScript not working as expected
提问by Salman A
Here is my javascript code:
这是我的 javascript 代码:
var prevDate = new Date('1/25/2011'); // the string contains a date which
                                      // comes from a server-side script
                                      // may/may not be the same as current date
var currDate = new Date();            // this variable contains current date
    currDate.setHours(0, 0, 0, 0);    // the time portion is zeroed-out
console.log(prevDate);                // Tue Jan 25 2011 00:00:00 GMT+0500 (West Asia Standard Time)
console.log(currDate);                // Tue Jan 25 2011 00:00:00 GMT+0500 (West Asia Standard Time)
console.log(prevDate == currDate);    // false -- why oh why
Notice that both dates are the same but comparing using ==indicates they are not the same. Why?
请注意,两个日期相同,但比较 using==表明它们不相同。为什么?
回答by nss
I don't think you can use ==to compare dates in JavaScript. This is because they are two different objects, so they are not "object-equal". JavaScript lets you compare strings and numbers using ==, but all other types are compared as objects.
我认为你不能用==JavaScript 来比较日期。这是因为它们是两个不同的对象,所以它们不是“对象相等”的。JavaScript 允许您使用 比较字符串和数字==,但所有其他类型都作为对象进行比较。
That is:
那是:
var foo = "asdf";
var bar = "asdf";
console.log(foo == bar); //prints true
foo = new Date();
bar = new Date(foo);
console.log(foo == bar); //prints false
foo = bar;
console.log(foo == bar); //prints true
However, you can use the getTimemethod to get comparable numeric values:
但是,您可以使用该getTime方法来获取可比较的数值:
foo = new Date();
bar = new Date(foo);
console.log(foo.getTime() == bar.getTime()); //prints true
回答by nybbler
Try comparing them using the date method valueOf().  This will compare their primitive value underneath instead of comparing the date objects themselves. 
尝试使用日期方法比较它们valueOf()。这将比较它们下面的原始值,而不是比较日期对象本身。
Example: 
console.log(prevDate.valueOf() == currDate.valueOf());  //Should be true
例子: 
console.log(prevDate.valueOf() == currDate.valueOf());  //Should be true
回答by mplungjan
console.log(prevDate.getTime() === currDate.getTime());
(as nss correctly pointed out, I see now) Why I use === here? have a look Which equals operator (== vs ===) should be used in JavaScript comparisons?
(正如 nss 正确指出的,我现在明白了)为什么我在这里使用 ===?看看在 JavaScript 比较中应该使用哪个等于运算符 (== vs ===)?
回答by Gajahlemu
Dont use == operator to compare object directly because == will return true only if both compared variable is point to the same object, use object valueOf() function first to get object value then compare them i.e
不要使用 == 运算符直接比较对象,因为 == 仅当两个比较变量都指向同一个对象时才会返回 true,首先使用 object valueOf() 函数获取对象值,然后比较它们,即
var prevDate = new Date('1/25/2011');
var currDate = new Date('1/25/2011');
console.log(prevDate == currDate ); //print false
currDate = prevDate;
console.log(prevDate == currDate ); //print true
var currDate = new Date(); //this contain current date i.e 1/25/2011
currDate.setHours(0, 0, 0, 0);
console.log(prevDate == currDate); //print false
console.log(prevDate.valueOf() == currDate.valueOf()); //print true
回答by Júlio Santos
JS compares dates using the >and <operators. If a comparison returns false, they're equal.
JS 使用>和<运算符比较日期。如果比较返回 false,则它们相等。

