Date Comparison in javascript

Expert User Verified

Date Comparison in javascript can be used when there are a sell start date and end date of any product or item.

<script type="text/javascript">
function FromDate(getID) {
                                                  
       document.getElementById('SalesEndDate').value = "";
       var dateString = document.getElementById(getID).value;
       var target = new Date(dateString);
       var now = new Date;
       
       if(now<=target)
       {
           return true;
       }
       else
       {
           alert("Starting date must be current or future date.");
                   document.getElementById(getID).value = "";
                  
                   return false;
       }
       
   }
function ToDate(getID) {
       var dateString = document.getElementById(getID).value;
       var toDate1 = new Date(dateString);
       var datestring1 = document.getElementById('SalesFromDate').value;
        var FromDate = new Date(datestring1);
        if(FromDate<=toDate1)
        {

            return true;
        }
        else
        {
           alert("End date must be Start or future date.");
                    document.getElementById(getID).value = "";
                    
                    return false;
        }

    }
</script>

There are two functions fromdate and todate which accept the parameter as an id of the text field.

<script type="text/javascript"> function FromDate(getID) { document.getElementById('SalesEndDate').value = ""; var dateString = document.getElementById(getID).value; var target = new Date(dateString); var now = new Date; if(now<=target) { return true; } else { alert("Starting date must be current or future date."); document.getElementById(getID).value = ""; return false; } } function ToDate(getID) { var dateString = document.getElementById(getID).value; var toDate1 = new Date(dateString); var datestring1 = document.getElementById('SalesFromDate').value; var FromDate = new Date(datestring1); if(FromDate<=toDate1) { return true; } else { alert("End date must be Start or future date."); document.getElementById(getID).value = ""; return false; } } </script>

Comments

Leave a Comment