Compare dates and validate date using javascript

Below is the javascript code to compare the dates and also to check the entered date is valid date or not

function comparedates()
{

   
    var fromdate=document.getElementById("FromDate").value;
    var todate=document.getElementById("todate").value;
   


if(trim(document.getElementById("fromdate").value)=="")
{
    alert('Please enter valid From date');
    return false;

    }

if(trim(document.getElementById("todate").value)=="")
{
    alert('Please enter valid Todate');
    return false;

    }


if(trim(document.getElementById("fromdate").value)!="")
{

if(!isDate(trim(document.getElementById("fromdate").value)))
{
alert("Please Enter BADATE"+in1+"  in (dd-mm-yyyy) Format");
return false;
}}




    a = fromdate.split('-');
    b = todate.split('-');
    var sDate = new Date(a[2],a[1]-1,a[0]);
    var eDate = new Date(b[2],b[1]-1,b[0]);

   
    if (sDate <= eDate )
    {
       
        return true;
    }
    else
    {
        el.errors.push("BADATE in row"+in1+" cannot be after respective DOJ");
        return false;
    }
}








//Function to check the valid date Format


function isDate(dateval)
{
   
   
   
   
   
    var strDay   = dateval.substring(0,2);
    var strMonth = dateval.substring(3,5);
    var strYear  = dateval.substring(6,10);
   
    var check0   = dateval.substring(2,3);
    var check1   = dateval.substring(5,6);
   
   

    if(check0 !="-")
        return false;
   
    if(check1 !="-")
        return false;
   
   
    if (isNaN(strDay) || isNaN(strMonth) || isNaN(strYear) )
        return false;
   
   
   
   
    var iDate = parseInt(1*strDay);
    var iMonth = parseInt(1*strMonth);
    var iYear = parseInt(1*strYear);
   
   
   
   
   
   
    if ((iDate > 0 && iDate < 32) && (iMonth > 0 && iMonth < 13) && (iYear > 1900 ))
        {
            if(iMonth==2 || iMonth==4 || iMonth==6 || iMonth==9 || iMonth==11)
                {
                    if(iMonth==2)
                        {
                            if(iYear %4 == 0 && iYear %100 != 0 || iYear %400 == 0)
                                {
                                    if(iDate>29)
                                        {
                                            return false;
                                        }
                                    }
                                   
                                    else if(iDate>28)
                                        {
                                            return false;
                                        }
                                       
                                    }
                                    if(iDate>30)
                                        {
                                            return false;
                                        }  
                                    } 
                                   
       }
            
      else
          {return false;}
                                   
      return true;                             
  }








No comments:

Post a Comment