JavaScript日期比较不等于 [英] JavaScript Date Comparisons Don't Equal

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

问题描述

我已经尝试搜索有类似问题的人,但没有找到任何内容。

I've tried searching for people with similar questions, but haven't found anything.

我在JavaScript中有两个日期,两者都设置为相同的值。 ..等式测试失败==,但是> =和< =评估为真。

I have two dates in JavaScript, both set to the same value... Equality Testing fails on ==, but >= and <= evaluate true.

以下是我玩的代码:

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?

我测试的是Internet Explorer(9),Firefox 5+和Chrome。

I tested this is Internet Explorer (9), Firefox 5+, and Chrome.

更新:

所以有两个人对我的问题发表了很好的答案,我感谢你们两个:xdazz和DaveRandom。我在stackoverflow.com上读过类似问题的早期帖子,一个人说日期对象可以像其他任何人一样进行比较,而我发现的任何其他例子总是做一个<或者>比较的类型,从来没有完全相等,所以我无法建立连接,为什么我做错了。

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.

感谢你们两个,和发布类似答案的其他人。

Thanks to you two, and the others that posted similar answers.

推荐答案

当你使用< = 或> = 比较两个日期对象,它们通过 valueOf 进行比较,这与<$相同c $ c> getTime for Date。

When you use <= or >= to compare two date objects, they are compared via valueOf, which is the same as getTime for Date.

但是当你使用 == 时,它们是同一类型的两个不同对象,因此返回false。

But when you use ==, they are two different objects of the same type, so it returns false.

添加了一些示例:

> 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

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

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