var d1 = new Date(); var d2 = new Date(d1);
You can also use Date() constructor to create date from strings.
var d1 = new Date('2022-05-22'); var d2 = new Date("March 21, 2022");
Once you have created date objects, you can compare it using comparison operators such as ==, !=, ===, and !==.
But for this purpose, you need to call getTime() function on your Date object. Here are a couple of examples to check if our dates d1 and d2 are same or different.
var same = d1.getTime() === d2.getTime(); var notSame = d1.getTime() !== d2.getTime();
Please note, you cannot use comparison operators directly on date objects, it may give you wrong output.
var d1 = new Date(); var d2 = new Date(d1); console.log(d1 == d2); // prints false (wrong!) console.log(d1 === d2); // prints false (wrong!) console.log(d1 != d2); // prints true (wrong!) console.log(d1 !== d2); // prints true (wrong!) console.log(d1.getTime() === d2.getTime()); // prints true (correct)
How to Overwrite Input File in Awk
How to Read Command Line Arguments in NodeJS