English 中文(简体)
Java 日期比较 Don t Equal [duplicate]
原标题:JavaScript Date Comparisons Don t Equal [duplicate]

我试图寻找有类似问题的人,但 have没有发现任何东西。

我在 Java有两个日期,两个日期都相同。 平等测试失败了=、但又不了;=和lt;=评价真实情况。

下面是我正在做的法典:

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?

我测试了因特网探索者(9)、5+和 Chrome。

<>Update:

因此,有两人对我的问题作了很大的回答,我感谢两人: x和Dave Random。 我读过了早先关于 st流问题的一个职位。

感谢你们两人和其他人作了类似的回答。

最佳回答

当您使用<代码><=或>>=比较两个日期物体时,通过<代码>价值的Of加以比较,该编码与gettime在日期相同。

但是,当你使用<条码>=时,它们是两种同类不同的物体,因此是假的。

添加一些例子:

> 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
问题回答

我想,你们会做些什么?

var startDate = (new Date( 2011, 7, 30, 0, 0, 0, 0 )).getTime();
var dt = (new Date( 2011, 7, 30, 0, 0, 0, 0 )).getTime();

在上面,你会发现它行之有效。

The getTime() method returns the date as an integer, what you are doing there is comparing objects, rather than specific values.

http://www.ohchr.org。 以上固定代码

这是 Java文的许多不合逻辑的部分之一。 您可以通过将日期改为.gettime(<>/code>)来解决这一问题,从该日期到1970年1月1日00:00时,该日期将回去多少毫米秒。

if (a.getTime() === b.getTime()) ...

<代码>=在物体本身上的操作者将永远不实,因为这两个物体确实是不同的 mu。

PS: Don t use =营运人,附印本。 页: 1 它把“民主”一词改为整个新层次。 严重。 问题在于适用所有类型的转换,这种转换只是毫无意义的,例如,您最后采用1”==1,x=false && (x?1:2)==1。 或甚至a=b && b=c && a!=c 是真实的(例如,x=[ ]a=***, b=1, c=[l])。 仅无t use =。 页: 1 它是“FU”。 全面。

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 /> );




相关问题
Mysql compaire two dates from datetime?

I was try to measure what is faster and what should be the best way to compare two dates, from datetime record in MySql db. There are several approaches how to grab a date from date time, but I was ...

iPhone Date Picker rolls over to 2010 on December 27th?

So I implemented a UIDatepicker in one of my applications for scheduling and autodialing teleconferences... everything is pretty much ready to go except, while testing I noticed that when the date ...

Convert date Python

I have MMDDYY dates, i.e. today is 111609 How do I convert this to 11/16/2009, in Python?

specifying date format when using $form->inputs() in CakePHP

I am wondering if there is a way to specify the date format in the forms created using CakePHP s $form->inputs(); Please note that this is not the individual $form->input() but instead $form->inputs() ...

NSDateFormat, super simple! Where am I screwing up?

Ok, this is really simple, maybe I m a getting a bit burnt out, but seems like it should work, Query XML feed, put out date string, format, display in a cell. The issue is I m a getting a NULL ...

sqlite writing a date into an email

I am exporting a date value from sqlite and placing it into an email. The date appears like this 279498721.322872 I am using Objective C in an Iphone App. Does anyone know how to make this export ...

热门标签