Add to collection
Expert User

Expert User

Verified

Date Comparison in javascript

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

Try Yourself in Code Editor    

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>

<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 0

  • Sorry!! No comment posted yet. Become the first user to give comment.
Add a comment

Would you like to write for w3 sniff?

Become part of an amazing group of contributors and authors and start writing for us

Join Now
<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>
Feedback submitted successfully.