Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
Please help me...
i want to check user enter date and system current date difference greater than 15 years. means user cannot enter the future date or current date. if user enter date and system current date difference less than 15 years then error message "please enter 15 years back date from the current date".

Thanks in advance.....
Posted

Try something like:
C#
DateTime enteredDate = Convert.ToDate(txtBox.Text);
DateTime currentDate = DateTime.Now;

TimeSpan diffResult = currentDate.Subtract(enteredDate);
int years = (int) (diffResult.Days / 365.25); // leap year considered
if (years >= 15)  //if difference greater than 15 years
{
    // do your stuff 
}


Details here:
DateTime.Subtract Method (TimeSpan)[^]
TimeSpan Structure[^]
 
Share this answer
 
Comments
dA.d 2-May-12 5:36am    
thanks for rply..
in this code when entered after 19 date means 20,21,22.....like then error is String was not recognized as a valid DateTime.
plz solve this problem..
Sandeep Mewara 2-May-12 7:00am    
Date format matters. You need to debug and see why it is so.
dA.d 2-May-12 8:25am    
thanks for help...
Sergey Alexandrovich Kryukov 2-May-12 14:04pm    
Answered. My 5.
--SA
Sandeep Mewara 3-May-12 1:08am    
Thanks SA.
Please use Range validator control to check the things. You can get the code to find the solution.

Client Side:
XML
<asp:TextBox ID="txtDate" runat="server" />
        <asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="txtDate"  Type="Date" ErrorMessage="Please enter 15 years back date from the current date." />




Server Side:

C#
protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                RangeValidator1.MinimumValue = DateTime.Now.AddYears(-100).ToShortDateString();
                RangeValidator1.MaximumValue = DateTime.Now.AddYears(-15).ToShortDateString();
            }
        }
 
Share this answer
 
Comments
dA.d 2-May-12 1:57am    
thanks for rply...
i am using above code and entering some dates like.20/05/1990, 20/05/2013
then showing error message, plz explain me which date i can insert in textbox..?
Ravinder_Verma 3-May-12 4:51am    
Dear you need to change the date format. My solution is based on mm/dd/yyyy format. Let me know if you need more information.

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