JavaScript Date comparison | String, time, Operators

You can do JavaScript Date comparison by operators and require to use date.getTime(). The getTime() function converts the given date objects into the numeric value we can directly compare them.

Examples of JavaScript Date comparison

The Date object will construct one for each date, then compare them using the ><<= or >=.

The ==!====, and !== operators require you to use date.getTime().

Compare 2 Dates in JS

Output:

JavaScript Date comparison

Compare date with the current date in javascript

Given string will take automatic time on converting in date object. So Current date will be greater in below example.

Output:

How to JavaScript date comparison without time?

Do not want to check the time part of the two dates, you can get the date part alone from dates and compare it?

Do clearing the time using Date.setHours:

Complete Example code

Output: mydate is greater than or equal to current date

How to javascript compare date strings?

Do comment if you have any suggestions or doubts on this tutorial.

Note: The All JS Examples codes are tested on the Safari browser (Version 12.0.2) and Chrome.
OS: macOS 10.14 Mojave
Code: HTML 5 Version


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.