SoFunction
Updated on 2025-04-03

JavaScript time comparison implementation code

In web development, sometimes it is necessary to compare the time in the input box (mainly the start time and the end time). After searching online, I found that many of them are invalid. The following methods are indeed effective after verification by me. I will share them here. (Please pay attention to the red section)

function ValidtorTime(){     

        var   d1   = new  Date(('txbFromDate').(/\-/g, "\/"));
        var   d2   = new  Date(('txbToDate').(/\-/g, "\/"));       
        if(d1>d2)
        {
alert("The end time must be after the start time!");
            return false;
        }   
      return true;
    }