How To Comparing Dates & Time Using JavaScript?

Nov 22, 2021 . Admin



Hello Friends,

Now let's see example of how to comparing dates and time example. We will check how to comparing dates and time. This is a short guide on comparing dates and time in javascript. Here you will learn how to comparing dates and time. Let's get started with how to comparing dates and time in javascript.

Here i will give you many example how to comparing dates and time using javascript.

Example : 1
<html>
    <head>
        <title>How To Comparing Dates & Time Using JavaScript? - MyWebtuts.com</title>
    </head>
    <body>
        <h3>How To Comparing Dates & Time Using JavaScript? - MyWebtuts.com</h3>
        <script>
            function a1()
            {
                var b1=new Date('2021-07-5'); 
                var b2=new Date('2021-08-7'); 
                
                if(b1>b2)
                {
                    document.write("True, First date is greater than second date");
                }
                else if(b1<b2)
                {
                    document.write("False, Second date is smaller than the first");
                }
                else
                {
                    document.write("Both date are same and equal");
                }
            }
            a1();
        </script>
    </body>
</html>  
Output :
False, Second date is smaller than the first
Example : 2
<html>
    <head>
        <title>How To Comparing Dates & Time Using JavaScript? - MyWebtuts.com</title>
    </head>
    <body>
        <h3>How To Comparing Dates & Time Using JavaScript? - MyWebtuts.com</h3>
        <script>
            var a1=new Date("Jan 15, 2021 12:10:45");
            var a2=new Date("May 25, 2021 12:11:32");
            
            if(a1>a2)
            {
                document.write("False, a1 date and time is smaller than a2 date and time");
            }
            else if(a1<a2)
            {
                document.write("True, a2 is greater in terms of both time and date");
            }
            else
            {
                document.write("Both date and time are same and equal");
            }
        </script>
    </body>
</html>  
Output :
True, a2 is greater in terms of both time and date

It will help you...

#Javascript