New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date comparsion bug #896

Closed
vadjs opened this Issue Apr 17, 2017 · 1 comment

Comments

Projects
None yet
3 participants
@vadjs

vadjs commented Apr 17, 2017

a = new Date();
b = new Date(); 

a == b;  //false
a < b;  //false
a <= b;  //true  

This must be very strange mechanics.

@claudepache

This comment has been minimized.

Show comment
Hide comment
@claudepache

claudepache Apr 17, 2017

Contributor

This is correct.

  • a == b is false, because a and b are not the same Date object. It does not test whether they represent the same moment.
  • On the other side, relational operators like < convert their operand to primitives before comparison. The primitive value of a Date object is the number of milliseconds since the epoch; so that they compare correctly the moments they represent.

The root issue is that == has different meanings according to the type of its operands (compares references for objects, compares values for primitives). This is unfixable. Use something more explicit than ==, e.g. a.valueOf() == b.valueOf().

Contributor

claudepache commented Apr 17, 2017

This is correct.

  • a == b is false, because a and b are not the same Date object. It does not test whether they represent the same moment.
  • On the other side, relational operators like < convert their operand to primitives before comparison. The primitive value of a Date object is the number of milliseconds since the epoch; so that they compare correctly the moments they represent.

The root issue is that == has different meanings according to the type of its operands (compares references for objects, compares values for primitives). This is unfixable. Use something more explicit than ==, e.g. a.valueOf() == b.valueOf().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment