JavaScript 日期比较不等于
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7244513/
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 Comparisons Don't Equal
提问by Patrick
I've tried searching for people with similar questions, but haven't found anything.
我试过寻找有类似问题的人,但没有找到任何东西。
I have two dates in JavaScript, both set to the same value... Equality Testing fails on ==, but >= and <= evaluate true.
我在 JavaScript 中有两个日期,都设置为相同的值……平等测试在 == 上失败,但 >= 和 <= 评估为真。
Below is the code I have in play:
下面是我在玩的代码:
var startDate = new Date( 2011, 7, 30, 0, 0, 0, 0 );
var dt = new Date( 2011, 7, 30, 0, 0, 0, 0 );
if( startDate == dt )
document.write('They Equal<br />');
if( startDate > dt )
document.write('Start Date is > dt<br />');
if( startDate >= dt )
document.write('Start Date is >= dt<br />');
if( startDate < dt )
document.write('Start Date is < dt<br />');
if( startDate <= dt )
document.write('Start Date is <= dt<br />');
if( dt == startDate )
document.write('They Equal<br />');
if( dt > startDate )
document.write('dt > startDate<br />');
if( dt >= startDate )
document.write('dt >= Start Date <br />');
if( dt < startDate )
document.write('dt < Start Date <br />');
if( dt <= startDate )
document.write('dt <= Start Date <br />');
document.write( dt );
document.write( '<br />');
document.write( startDate );
Has anyone encountered anything like this, or am I doing something fundamentally wrong?
有没有人遇到过这样的事情,或者我做错了什么?
I tested this is Internet Explorer (9), Firefox 5+, and Chrome.
我测试了它是 Internet Explorer (9)、Firefox 5+ 和 Chrome。
Update:
更新:
So two people posted great answers to my problem, and I thank both of you: xdazz and DaveRandom. I had read an earlier post on stackoverflow.com on a similar question and a guy said that date objects could be compared like any others, and any other example I found always did a < or > type of compare, never a full equality so I wasn't able to make the connection as to why I was doing it wrong.
所以有两个人为我的问题发布了很好的答案,我感谢你们两个:xdazz 和 DaveRandom。我曾在 stackoverflow.com 上阅读过一篇关于类似问题的早期帖子,有人说日期对象可以像任何其他对象一样进行比较,而我发现的任何其他示例总是进行 < 或 > 类型的比较,从来没有完全相等,所以我无法就我做错的原因建立联系。
Thanks to you two, and the others that posted similar answers.
感谢你们两个,以及其他发布类似答案的人。
回答by xdazz
When you use <=
or >=
to compare two date objects, they are compared via valueOf
, which is the same as getTime
for Date.
当您使用<=
或>=
比较两个日期对象时,它们通过 进行比较valueOf
,这与getTime
日期相同。
But when you use ==
, they are two different objects of the same type, so it returns false.
但是当您使用 时==
,它们是相同类型的两个不同对象,因此它返回 false。
Added some examples:
添加了一些示例:
> new Date(2011, 7, 30, 0, 0, 0, 0) == new Date( 2011, 7, 30, 0, 0, 0, 0 )
false
> new Date(2011, 7, 30, 0, 0, 0, 0).getTime() == new Date( 2011, 7, 30, 0, 0, 0, 0).getTime()
true
> new Date(2011, 7, 30, 0, 0, 0, 0).valueOf() == new Date( 2011, 7, 30, 0, 0, 0, 0).valueOf()
true
> new Date(2011, 7, 30, 0, 0, 0, 0).valueOf() == new Date( 2011, 7, 30, 0, 0, 0, 0).getTime()
true
回答by DaveRandom
I think if you do
我想如果你这样做
var startDate = (new Date( 2011, 7, 30, 0, 0, 0, 0 )).getTime();
var dt = (new Date( 2011, 7, 30, 0, 0, 0, 0 )).getTime();
At the top of the script you will find it works.
在脚本的顶部,您会发现它有效。
The getTime()
method returns the date as an integer, what you are doing there is comparing objects, rather than specific values.
该getTime()
方法以整数形式返回日期,您在那里做的是比较对象,而不是特定值。
EDITFixed above code
编辑修复了上面的代码
回答by 6502
This is one of the many illogical parts of Javascript. You can work around this problem by converting the date to a number using .getTime()
that will return how many milliseconds passed from that date and 00:00:00 of Jan 1, 1970.
这是 Javascript 众多不合逻辑的部分之一。您可以通过将日期转换为一个数字来解决此问题,该数字.getTime()
将返回从该日期和 1970 年 1 月 1 日 00:00:00 过去的毫秒数。
if (a.getTime() === b.getTime()) ...
The ===
operator on the objects themselves will return always false because those two objects are indeed distinct mutable javascript objects.
===
对象本身的运算符将始终返回 false,因为这两个对象确实是不同的可变 javascript 对象。
PS: Don't use ==
operator with Javascript. Ever. What it does takes the term "crazy" to a whole new level. Seriously. The problem is that applies all sort of type conversions that simply make no sense and for example you end up with things like "1"==1
, x==false && (x?1:2)==1
or even a==b && b==c && a!=c
being true (e.g. with x=[]
or with a=[1], b=1, c=[[1]]
). Just don't use ==
. Ever. It's "FUBAR". Fullstop.
PS:不要==
在 Javascript 中使用运算符。曾经。它所做的将“疯狂”一词提升到一个全新的水平。严重地。问题是应用了所有类型的类型转换,这些类型转换根本没有意义,例如,您最终会得到类似"1"==1
,x==false && (x?1:2)==1
甚至a==b && b==c && a!=c
是 true 之类的东西(例如 withx=[]
或 with a=[1], b=1, c=[[1]]
)。只是不要使用==
. 曾经。是“福巴”。句号。
回答by Shef
var startDate = new Date( 2011, 7, 30, 0, 0, 0, 0 );
var dt = new Date( 2011, 7, 30, 0, 0, 0, 0 );
if( startDate.getTime() == dt.getTime() )
console.log('They Equal<br />');
回答by user3202283
<Script>
function checkval()
{
var strfromdate=document.frmadmin.txtfromdate.value;
strfromdate=strfromdate.split("/");
//alert(strfromdate[2]+" "+strfromdate[0]+" "+strfromdate[1]);
var strtodate=document.frmadmin.txttodate.value;
strtodate=strtodate.split("/");
var fromDate = (new Date( strfromdate[2], strfromdate[0], strfromdate[1], 0, 0, 0, 0 )).getTime();
var toDate = (new Date( strtodate[2], strtodate[0], strtodate[1], 0, 0, 0, 0 )).getTime();
//alert(fromDate+" "+toDate);
if(toDate<fromDate)
{
alert("To date should be greater than from date.");
document.frmadmin.txttodate.focus();
return false;
}
return true;
}
</Script>