You can use the compareAsc
function from date-fns
to compare two dates in JavaScript. This function returns an integer value that indicates whether the first date is before, after, or the same as the second date.
Here's an example:
index.tsx224 chars10 lines
In this example, we first import the date-fns
library using the require
function. We then create two Date
objects representing date1
and date2
.
To compare the two dates, we pass them as arguments to the compareAsc
function, which returns -1 to indicate that date1
is before date2
.
You can also use this function to compare the current date with another date:
index.tsx424 chars15 lines
In this example, we compare the current date with a future date, and use a conditional statement to print a message indicating whether the current date is before, after, or the same as the future date.
gistlibby LogSnag