Click here to Skip to main content
14,691,021 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
hi how to validate two date like first date is less then second date
thanks
Posted
Updated 13-Mar-20 21:57pm
Comments
[no name] 9-Jul-13 4:53am
   
Question should be clear!
Comparision in SQL or ASP.Net?

Declare @D1 datetime, @D2 datetime
set @D1 = '2013-07-05' --yyyy-mm-dd
set @D2 = '2013-07-09' --yyyy-mm-dd
select
    case
        when @D1 = @D2 then 'equal dates'
        when @D1 < @D2 then 'first date is less then second date'
        else 'Second date is less then first date'
    end

Happy Coding!
:)
   
v2
Hi...
Am taking two textboxes like: TextBox1 and TextBox2.
Now am comparing two textbox values like blo.
<asp:CompareValidator ID="CompareValidator1" runat="server"

        ControlToCompare="TextBox1" ControlToValidate="TextBox2"

        ErrorMessage="date2 must greater than date1" Operator="GreaterThan"

        Type="Date" CultureInvariantValues="True"></asp:CompareValidator>

So textbox2 values always greaterthan textbox1.its may helpful to u,thank u
   
Refer to this. Its too simple.

DateTime.Compare Method[^]

Regards.. :laugh:
   
function checkDate()
{
    var fromDate=document.getElementById("<%=tcstartdate.ClientID%>").value;
    var toDate=document.getElementById("<%=tcenddate.ClientID%>").value;
    var cataloguename=document.getElementById("<%=txtcataloguename.ClientID%>").value;
    var desc=document.getElementById("<%=txtdescription.ClientID%>").value;

    if(cataloguename.trim() == "")
    {
        alert("Please Enter Special name");
        document.getElementById("<%=txtcataloguename.ClientID%>").focus();
        return false;
    }
    if(fromDate == "")
    {
        alert("Please Enter Special start date");
        document.getElementById("<%=tcstartdate.ClientID%>").focus();
        return false;
    }
    if(toDate == "")
    {
        alert("Please Select Special End date");
        document.getElementById("<%=tcenddate.ClientID%>").focus();
        return false;
    }
   // alert(fromDate);
   // alert(toDate);

    var record_day1=fromDate.split("/");
    var sum1=record_day1[1]+'/'+record_day1[0]+'/'+record_day1[2];
    var record_day2=toDate.split("/");
    var sum2=record_day2[1]+'/'+record_day2[0]+'/'+record_day2[2];
    var record1 = new Date(sum1);
    var record2 = new Date(sum2);
    var d = new Date();
    var ddd =d.getMonth()+ 1 +'/'+d.getDate()+'/'+d.getFullYear();
    var dd=new  Date(ddd);
    if(record2 < record1)
    {
            alert("Invalid Date ! Please try again");
              document.getElementById("<%=tcstartdate.ClientID%>").focus();
            return false;
    }
    if(record2 < dd)
    {
          alert("Invalid Date ! Please try again");
             document.getElementById("<%=tcstartdate.ClientID%>").focus();
            return false;
    }
//    if(desc.trim() == "")
//    {
//        alert("Please enter description ");
//        document.getElementById("<%=txtdescription.ClientID%>").focus();
//        return false;
//    }

     var inputs = [];
     var i,c=0;
     grid=document.getElementById("<%=gridCatalogue.ClientID %>");
     inputs = document.getElementById("<%=gridCatalogue.ClientID %>").getElementsByTagName("input");
      var input=document.getElementsByTagName("input");
     for (i=0;i<grid.rows.length-1;i++)
       if(inputs[4*i].value!="")
       {
           c++;
        }
       if(c==0)
       {
            alert("Please enter atleast one product");
            return false;
       }
            //alert('came');
            var input=document.getElementsByTagName("input");
            var sample,hidid;
            for(var i=0;i<input.length;i++)
            {
                //alert(input[i].type);
                if(input[i].type=="hidden")
                    hidid=document.getElementById(input[i].id).value;
                if(input[i].type=="text")
                {
                    if(input[i].id.indexof("txtprice")>0)
                    {
                        sample=document.getElementById(input[i].id).value;
                        if(isnan(parseint(sample)) && !isnan(parseint(hidid)))
                        {
                            alert("Please enter price");
                            document.getElementById(input[i].id).focus();
                                return false;
                            }
                        else if(!isnan(parseint(hidid)))
                        {
                            var isfound = /^(?!^0*$)(?!^0*\.0*$)^\d{1,10}(\.\d{1,2})?$/.test(sample);
                            if(isfound)
                            {
                            }
                            else
                            {
                                alert('Invalid price ! please try again');
                                document.getElementById(input[i].id).value="";
                                document.getElementById(input[i].id).focus();
                                return false;
                            }
                        }
                    }
                }
            }
}



use this javascript function
   
try this javascript,...

<script type="text/javascript" language="javascript">
function ValidateSearch() {
var msg = document.getElementById('<%=lblListMsg.ClientID%>');
var FromDate = document.getElementById('<%=txtFromDate.ClientID%>').value;
var ToDate = document.getElementById('<%=txtToDate.ClientID%>').value;
var startDate = new Date(getJSDate(FromDate));
var endDate = new Date(getJSDate(ToDate));
            var curDate = new Date();
            if (startDate > curDate) {
                msg.innerHTML = "From date should not be greater than today's date";
                msg.style.color = "red";
                document.getElementById('<%=txtFromDate.ClientID%>').value = "";
                document.getElementById('<%=txtFromDate.ClientID%>').focus();
                return false;
            }
        if (endDate > curDate) {
                msg.innerHTML = "To date should not be greater than today's date";
                msg.style.color = "red";
                document.getElementById('<%=txtToDate.ClientID%>').value = "";
                document.getElementById('<%=txtToDate.ClientID%>').focus();
                return false;
            }
            if (startDate > endDate) {
                msg.innerHTML = "From date should not be greater than to date";
                msg.style.color = "red";
                document.getElementById('<%=txtFromDate.ClientID%>').value = "";
                document.getElementById('<%=txtFromDate.ClientID%>').focus();
                return false;
            }
            document.getElementById('<%=lblListMsg.ClientID%>').innerHTML = "";
            return true;
}
 </script>


to call javascript method

<asp:Button ID="btnSearchPO" runat="server"

    OnClientClick="return ValidateSearch()" />


Accept as answer if solve you problem.
   
Hi,
try this

declare @D1 date
declare @D2 date

set @D1='15-june-2013'
set @D2 ='15-apr-2013'


if DATEDIFF(day,@D1,@D2) > 0
(
select 'first date is lesser then second'
)
else if DATEDIFF(day,@D1,@D2) < 0
(
select 'second date is lesser then first'
)
else if DATEDIFF(day,@D1,@D2) = 0
(
select 'Both date are same'
)


I hope this will help you... :)
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900