Skip to content

Instantly share code, notes, and snippets.

@epicserve
Last active December 28, 2015 01:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save epicserve/7418915 to your computer and use it in GitHub Desktop.
Save epicserve/7418915 to your computer and use it in GitHub Desktop.
var date1 = new Date(2013, 10, 12);
var date2 = new Date(2013, 10, 12);
/*
When running the javascript in this file you get the following output ...
$ node js_date_test.js
Tue Nov 12 2013 00:00:00 GMT-0800 (PST)
Tue Nov 12 2013 00:00:00 GMT-0800 (PST)
false
false
false
false
Which didn't make sense, because the last two console.logs should be true.
However thanks to @isntitvacant I got this explanation.
>> Date objects provide a `valueOf()` method that returns their value as a
>> integer unix epoch millisecond timestamp and you can trigger `valueOf()`
>> with unary `+`
*/
console.log(date1);
console.log(date2);
console.log(date1 > date2);
console.log(date1 < date2);
console.log(date1 == date2);
console.log(date1 === date2);
/*
So putting what @isntitvacant said into practice. The following console.logs output ...
Tue Nov 12 2013 00:00:00 GMT-0800 (PST)
Tue Nov 12 2013 00:00:00 GMT-0800 (PST)
false
false
true
true
*/
console.log(Array(40).join("-"));
console.log(date1);
console.log(date2);
console.log(+date1 > +date2);
console.log(+date1 < +date2);
console.log(+date1 == +date2);
console.log(+date1 === +date2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment