Click here to Skip to main content
15,922,166 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have two text box named as from date and to date .
validation give todate mustbe greater than one year.
how to give?
Posted
Comments
R. Giskard Reventlov 22-Jul-11 3:29am    
What have you tried for yourself?
Uday P.Singh 22-Jul-11 3:32am    
do you mean fromdate and todate must be greater than 1 year? your question is not clear what do you want to do?

DateTime startdate = Convert.ToDateTime(txtDateFrom.Text);
DateTime endate = Convert.ToDateTime(txtDateTo.Text);
C#
if (DateTime.Compare(startdate, enddate) < 0)
            {


}
else
{
MIDL
lblErrorMessage.Visible = true;
             lblErrorMessage.Text = "Start date should be less than end date ";


}
 
Share this answer
 
Comments
jitendra kumar mahpatro 22-Jul-11 3:48am    
This check only less than from date but not more than one year(365 days)
Mohd Wasif 22-Jul-11 6:27am    
OK Do something like it.
DateTime sdate= Convert.ToDateTime(txtDateFrom.Text);
DateTime startdate = sdate.AddYears(1);
You can use the custom validation tags to do this. But you would have to do some javascript as well. This works well for checking if the text boxes have values as well, by using this:

VB
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Please enter date one"
               ValidationGroup="validationGroup" Display="None" ControlToValidate="txtDateOne" /> 


And then for the custom tag have something like this:
<asp:customvalidator id=""CustomValidator1"" runat=""server"" display=""None"" errormessage=""Please" make="" sure="" there="" is="" a="" one="" year="" mode="hold" xmlns:asp="#unknown" />                ValidationGroup="validationGroup" ClientValidationFunction="validateCorrespondence"></asp:CustomValidator>


In the client validation field you can pass the javascript function name that checks the two dates
 
Share this answer
 
try that way

C#
function getTimeDiff(){
    var txtBoxStartDate = document.getElementById('<%=txtBoxStartDate.ClientID%>');
    var txtBoxEndDate = document.getElementById('<%=txtBoxEndDate.ClientID%>');

    var dtStart = new Date(txtBoxStartDate.value);
    var dtEnd = new Date(txtBoxEndDate.value);

    var diff = Math.floor((dtEnd.getTime() - dtStart.getTime())/(1000*60*60*24));
   alert(''The diff between dates is " + diff )

}
 
Share this answer
 
Try:
if (todate >= fromdate.AddYears(1))
   {
   ...
   }

MSDN[^]


"Actually if i enter a date like 31-12-1984 then how it check 01-01-1985 the difference between two date is only one day."


Yes. And AddYears adds a whole year to the from date, making it 31-12-1985...
DateTime from = new DateTime(1984, 12, 31);
DateTime to = new DateTime(1985, 1, 1);
if (to >= from.AddYears(1))
{
    Console.WriteLine("Greater");
}
else
{
    Console.WriteLine("Less");
}
Prints "Less"
 
Share this answer
 
v2
Comments
jitendra kumar mahpatro 22-Jul-11 3:37am    
Actually if i enter a date like 31-12-1984 then how it check 01-01-1985 the difference between two date is only one day.
OriginalGriff 22-Jul-11 3:53am    
Answer updated
Hi,

check it in javascript if the second text box value greater than the first textbox value...


check this this for calculating age...convert it to your use.
function test()
{
// alert("hai");
debugger;


var bday =document.getElementById('<%=date.ClientID%>').value
var bmo =document.getElementById('<%=month.ClientID%>').value
var byr =document.getElementById('<%=year.ClientID%>').value


var byr;
var age;
var now = new Date();
var tday=now.getDate();
var tmo=(now.getMonth());
var tyr=(now.getFullYear());
{
if((tmo > bmo)||(tmo==bmo & tday>=bday))
{age=byr}
else
{age=byr+1}
alert("As of today, "+now+' \n'+", you are:"+(tyr-age)+ " years old");


}
}

regards,
shefeek
 
Share this answer
 
v2
Comments
Dalek Dave 22-Jul-11 5:37am    
He doesn't need javascript for this kind of problem, it is merely arithmetical.

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