JavaScript 日期比较
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2752532/
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
JavaScript date comparison
提问by user290870
Possible Duplicate:
Compare 2 dates with JavaScript
可能的重复:
用 JavaScript 比较 2 个日期
I haven't done much JavaScript. I am trying to compare two dates. From jconsole:
我没有做过很多 JavaScript。我正在尝试比较两个日期。来自 jconsole:
a = ["01/01/2010","01/02/2010","01/03/2010"]
date1 = new Date('01/02/2010')
Sat Jan 02 2010 00:00:00 GMT-0800 (PST)
date2 = new Date(a[1])
Sat Jan 02 2010 00:00:00 GMT-0800 (PST)
date1 == date2
false
Can someone tell me why this does not match?
有人能告诉我为什么这不匹配吗?
回答by Daniel Vassallo
Your comparison is returning false because date1and date2are simply references to different objects, and you are actually comparing these references.
您的比较返回 false,因为date1和date2只是对不同对象的引用,而您实际上是在比较这些引用。
To do a proper comparison of the date values, you can use the getTime()method as follows:
要正确比较日期值,您可以使用以下getTime()方法:
date1.getTime() === date2.getTime(); // returns true

