By Harsh Tanwar
Today we will learn How to compare two dates. We compare if a date is after or before another, if the date is today, how many days there are between dates
This means that you don't need an external library to perform rudimentary checks and operations, which makes it easier to perform these things in Vanilla JS.
Even though this implementation seems a bit simplistic, the addition of the Date class was quite a big improvement, since there was finally a level of abstraction between developers and raw dates.
Now, let's look at different ways to compare two dates using
We can use comparison operators like
> two compare two
Date objects, and under the hood, their time counters are effectively compared. You're effectively comparing two integer counters
1 9/2/2021 is greater than 9/2/2000 2 10/6/2021 is greater than 10/5/2021 3 Both dates are equal
As we can see, comparing dates just boils down to converting the provided strings into Date
objects and comparing them with an appropriate comparison operator.
Submitted by Harsh Tanwar (harshtanwar)
Download packets of source code on Coders Packet