This content originally appeared on CodeSource.io and was authored by Ariessa Norramli
In this article, you will learn how to Compare Dates in Javascript.
Let’s say you have two dates: 14 / 2 / 2020 and 14 / 2 / 2021.
var date1 = new Date(2021, 02, 14);
var date2 = new Date(2020, 02, 14);
In order to compare dates, you can use the getTime() method and a strict equality comparison ===.
var date1 = new Date(2021, 02, 14);
var date2 = new Date(2020, 02, 14);
if (date1.getTime() === date2.getTime())
console.log("The dates are equal");
else
console.log("The dates are not equal");
Note: The getTime method functions by returning the number of milliseconds between the midnight of 1/1/1970 and the supplied date.
The post How to Compare Dates in Javascript appeared first on CodeSource.io.
This content originally appeared on CodeSource.io and was authored by Ariessa Norramli
Ariessa Norramli | Sciencx (2021-02-14T17:10:53+00:00) How to Compare Dates in Javascript. Retrieved from https://www.scien.cx/2021/02/14/how-to-compare-dates-in-javascript/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.