0

I am trying to do some chrome console coding to implement a calendar in javascript, but I am facing a very basic problem.

a = new Date(2017, 9, 9);
// Mon Oct 09 2017 00:00:00 GMT+0530 (IST)
b = new Date(2017, 9, 9);
// Mon Oct 09 2017 00:00:00 GMT+0530 (IST)
console.log(a == b);

But they are not equating:

a == b 
false

It has to be some stupid mistake I am making but can't seem to find out, Any help please??

MinusFour
  • 13,913
  • 3
  • 30
  • 39
  • 1
    That's because the objects are not the same. – Teemu Oct 25 '17 at 18:14
  • What value is there in knowing 2 dates are the exact same instant in time? and how close do 2 instants in time have to be before you consider them the same instant? This smells like bad design.. – anthonybell Oct 25 '17 at 18:47

5 Answers5

5

getTime() returns a number representing the milliseconds elapsed between 1 January 1970 00:00:00 UTC and the given date. So use this to determine whether two Dates are the same.

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

andi
  • 6,442
  • 1
  • 18
  • 43
  • 1
    It's more efficient to compare integers (like this) than strings (like the rest of the answers). – James Oct 25 '17 at 18:24
  • It is also always correct, converting the date to a string doesn't retain all the information – pfg Oct 25 '17 at 18:27
  • it worked, but why printing a.getTime() gives something like this: 1510252200000 Edit: got it. thanks – Ritambhara Chauhan Oct 25 '17 at 18:38
  • @RitambharaChauhan because that's how many milliseconds are between 1 January 1970 00:00:00 UTC and the Date you created. – andi Oct 25 '17 at 18:40
1

Yea the problem here is that a and b are both pointers to a different memory locations because they are date objects not strings so they are not equal. You could however ask if the toString() version of them are equal

1

To start with, even the below is not true:

var obj1 = {};
var obj2 = {};
console.log(obj1 == obj2); // false; why? They refer different instances

Date objects are compared by reference when using == check, not by their value.

So, what is the way out to use these operators?

Convert the Date object to types where == are supported to do this check. Can be done by converting them to string or numeric representations where the == checks based on value, not by reference.

Here is a sample snippet with code comments to understand further:

// Objects
var obj1 = {};
var obj2 = {};
console.log(obj1 == obj2); // false; why? They refer different instances

obj2 = obj1;
console.log(obj1 == obj2); // true; why? They refer the same `location` / instance

// Exceptions - String for example
var str1 = `Hello`;
var str2 = `Hello`;
console.log(str1 == str2); // true; Types like number, string are checked for values not for their memory reference
L J
  • 5,249
  • 3
  • 19
  • 29
0

You can use toString

a.toString() === b.toString()

Dmitriy Kovalenko
  • 3,437
  • 3
  • 19
  • 39
  • You shouldn't use `toString` to compare dates. `toString` functions are not required to always give full information about the object they were called on. For example, `(new Date(100)).toString() == (new Date(101)).toString()` is true, while `(new Date(100)).getTime() == (new Date(101)).getTime();` is false – pfg Oct 25 '17 at 18:26
0

JavaScript does not have equality by value for objects. Objects are literally everything that is not a primitive. The list can be found here. Anything that is (or can be) created using the new keyword is an object.

Object equality is done by identity. The two pointers (variables) have to be pointing at the exact same memory address(es) in order for equality to work the way you expect it to.

Equality for primitives compares by value. As the other answer states, if you convert it to a primitive string first, you can do a direct value-to-value comparison.

a = new Date(2017, 9, 9)
b = new Date(2017, 9, 9)
a.toString() === b.toString()

Side note: string objects exist, but you have to explicitly declare them with new String

Andrew
  • 7,201
  • 5
  • 25
  • 34