Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi,
I'm working on from date and To date validation.How to chk whether to date should be more than from date.

Thanks and Regards,
M.G.Balamurugan
Posted
Comments
[no name] 25-Nov-14 1:38am    
Please improve question.. mention is it web app or desktop
Bala1989 25-Nov-14 1:45am    
Working on Webbased application

What more than:
C#
d1 = DateTime.Now;
d2 = DateTime.Now.AddDays(12);

if (d2 > d1)
{
    // date order ok
}
else
{
   // date order reversed
   // swap the dates ?
}
Do you want ?

Do you want to specify minimum and/or maximum intervals (TimeSpan) between the two Dates ?

Do you want a very specific DateTime validator where the criteria are fixed, or do you want a general purpose validator you can pass parameters into that handle a variety of conditions ?

How about excluding certain Date intervals if they include any weekend day, or any specific range of hours of day, or night ?

Let's see some of your code.
 
Share this answer
 
Comments
Bala1989 25-Nov-14 1:56am    
I got my solution and posted it and it is working for me.
Get date from controls using DateTime.TryParse method[^] and compare them. That's all!
 
Share this answer
 
The below method should show in button validation,
C#
string valid = CheckValid();
if (valid == "")
{ 
    errormsg.Text = valid;
}


The method should define here
C#
private string CheckValid()
{
    try
    {
        string valid;
        if (txtFromDate.Text != string.Empty && txtToDate.Text != string.Empty)
        {
            DateTime startdate = DateTime.Parse(txtFromDate.Text);
            DateTime enddate = DateTime.Parse(txtToDate.Text);
            if (startdate > enddate)
            {
                txtFromDate.Focus();
                return valid = "From Date is Should be less than To Date";
            }
            else
            {

            }
        }

        return valid = "";
    }
    catch (Exception Ex)
    {
        throw (Ex);
    }
}
 
Share this answer
 
v2
Comments
Maciej Los 25-Nov-14 2:06am    
Is it an answer?
Bala1989 25-Nov-14 2:14am    
s s i have pasted the entire process and its working fine...

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