Date comparison giving incorrect result

I am getting DateTime from API response and for some data, I am getting dummy date like below :

0001-01-01T00:00:00

Now, I want to check If I get a dummy date like above then I want to store null in my variable else the exact date.

I have a code like below but getting “NAN” :

console.log("2017-01-01T00:00:00" == "0001-01-01T00:00:00" ? null : moment("2017-01-01T00:00:00", "DD/MM/YYYY").year()); //output Nan

I am not getting whats the issue here. Can someone please help me?

Answer

If you try like this, passing the actual format to moment function, it ouputs 2017 in your case.

console.log("2017-01-01T00:00:00" == "0001-01-01T00:00:00" ? null : moment("2017-01-01T00:00:00", "YYYY-MM-DDT h:mm:ss").year()); //outputs 2017

If you want to compare dates irrespective of datetime format you can do like this:

console.log(moment('2017-01-01T00:00:00').format('YYYY-MM-DD')); //date1
console.log(moment('2017-01-01T00:00:00').format('YYYY-MM-DD')); //date2

And you can compare the 2 dates now, by setting what format you want, irrespective to the original format

Included also the isSame version from comments:

moment('2010-10-20').isSame('2010-10-20'); // true

But if you have a particular case you should consider reading the moment.js documentation.