All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Date Comparison In JavaScript

Last Updated : Jul 1, 2023

Date Comparison In JavaScript

In this tutorial we will show you the solution of date comparison in JavaScript, here we needs to use getTime() method in javascript for convert date with milliseconds timestamp format.

The getTime() method usually returns the number of milliseconds since the ECMAScript epoch.

We can use this method to help assign a date and time to another Date object.

By using method we comparing specified two date with milliseconds format then printing result on webpage.

Step By Step Guide On Date Comparison In JavaScript :-

Here in script we defined two variables ‘d1,d2’ for holding two different dates which we specified then we printed those dates on console for user clarification.

Appending getTime() method with ‘d1,d2’ variables will returns milliseconds of date format then using ‘===’ operator we comparing both dates using if condition then respective result message will printed on webpage by ‘document.write()’ method.

<!DOCTYPE html>
        <title>DATE COMPARISON</title>
            var d1=new Date();
            var d2=new Date("September 24, 1998 03:24:00");
            if(d1.getTime() === d2.getTime()){
                document.write("Both Dates Are Same");
                document.write("Both Dates Are Not Same");
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  5. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  6. In script variable ‘d1’ holds current date in string format, variable ‘d2’ holds specified date ‘sep 24 1998 03:24:00’ then we printed both dates on console for any verifications.
  7. For open console we need to right click on browser then select ‘inspect’ option from list and panel will open there we need to choose console tab.
  8. Using if condition we comparing both dates with the help of getTime() method which is converting them into milliseconds format so we can compare dates easily.
  9. If returns true means both dates are same so we printed message ‘Both Dates Are Same’ on webpage by ‘document.write()’ method.
  10. Otherwise it returns false so it will prints ‘Both Dates Are Not Same’ message on webpage by ‘document.write()’ method.
  11. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to compare dates using JavaScript.

When we executes program on browser we can see the result message ‘Both Dates Are Not Same’ on webpage because we specified two different dates if we specified two dates are same then result will changed appropriately we can see on webpage clearly when we modify them as same.

I hope this tutorial on date comparison in JavaScript helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪