Last active
December 28, 2015 01:09
-
-
Save epicserve/7418915 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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