diff --git a/README.md b/README.md index d2791e2..d88f8e6 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ and pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud). -_962 TILs and counting..._ +_963 TILs and counting..._ --- @@ -314,6 +314,7 @@ _962 TILs and counting..._ - [Check Classes On A DOM Element](javascript/check-classes-on-a-dom-element.md) - [Check If Something Is An Array](javascript/check-if-something-is-an-array.md) - [Check The Password Confirmation With Yup](javascript/check-the-password-confirmation-with-yup.md) +- [Compare The Equality Of Two Date Objects](javascript/compare-the-equality-of-two-date-objects.md) - [Computed Property Names In ES6](javascript/computed-property-names-in-es6.md) - [Conditionally Include Pairs In An Object](javascript/conditionally-include-pairs-in-an-object.md) - [Configure Jest To Run A Test Setup File](javascript/configure-jest-to-run-a-test-setup-file.md) diff --git a/javascript/compare-the-equality-of-two-date-objects.md b/javascript/compare-the-equality-of-two-date-objects.md new file mode 100644 index 0000000..f81237b --- /dev/null +++ b/javascript/compare-the-equality-of-two-date-objects.md @@ -0,0 +1,22 @@ +# Compare The Equality Of Two Date Objects + +Equality can always feel like a bit of a moving target in JavaScript. Comparing +two objects, even if visually/conceptually identical, will resolve to being not +equal. + +To compare two `Date` objects, you first need to convert them to something we +can check for equality -- numbers. This can be done with `getTime()`. + +```javascript +> now = Date.now() +> today1 = new Date(now) +> today2 = new Date(now) +> today1 === today2 +false +> today1.getTime() === today2.getTime() +true +``` + +With `Date` objects, you can directly use `<`, `>`, `<=`, and `>=`. + +[source](https://stackoverflow.com/questions/492994/compare-two-dates-with-javascript)