A common task to do as a frontend developer is to process Date objects, and frequently that means to compare them. Today, I am going to show you the best way to compare dates and some tips that will help you to get the results you expected. This is very important because for example if you want to develop a method that sorts by a Date property, I am pretty sure you will face this issue and you will spend too many hours trying to figure out the actual reason about the error.
Then, you want to compare them, so you would probably think this code will print "a equals b":
But my friend I have a bad new for you, previous code will always print "a NOT equals b", because Dates cannot be compared as simple types.
Instead of comparing dates using the operators directly, a good practice is to compare the dates but using the milliseconds format. You can obtain milliseconds format using the getTime( ) function.
Now, the result is correct, previous code will print "a equals b".
Creating dates from String (parsing to Date)