Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi
I have two text boxes in which the start date and end date is to be filled.The date, month can be different but the year should be same.So i just want to put a validation for the user that they should put the same year else a error message generate showing the year is not matching.
Posted

Try this..
C#
protected void Button1_Click(object sender, EventArgs e)
{
    compareDate();
}
void compareDate()
{
    DateTime year1 = DateTime.Parse("13/8/2010"); //Date Format: dd/MM/yyyy
    DateTime year2 = DateTime.Parse("9/2/2012"); //Date Format: dd/MM/yyyy

    if (year1.Year != year2.Year)
    {
        ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "msg", "alert('Year is not matching!');", true);
    }
}
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 31-Dec-12 1:34am    
Basically, that's it, my 5. And if the time data is written in a different format, there are different forms of Parse or TryParse.
—SA
ridoy 31-Dec-12 1:41am    
+5,but SA is right..TryParse would be a good one
prashant patil 4987 31-Dec-12 1:44am    
ya... i agree with both of you SA and ridoy.. try parse is also good for parsing.
falque 31-Dec-12 7:48am    
Thanks for the concern.Its working fine now but there is one more requirement as when the user put the value in the second text box it automatically compares the value of year in both the text boxes if its similar its ok else produce an error message showing the years are not mathching. Any help would be appreciated.Thanking You
prashant patil 4987 1-Jan-13 0:24am    
@faique: can you clear your quetion?
You have to first tell the user the format of date they will have to enter in the text box (such as 'dd/mm/yyyy'). Its best to instead use a DateTimePicker control instead of textbox because then the user will click and choose the date where as if the user enters a date in a textbox they can make a mistake.

if let's say you are using a textbox, then in the event handler method where you are using the input you can use the following code:

C#
DateTime dateTime1 = DateTime.Parse(textBox1.Text); //Where I suppose the user enters "dd/mm/yyyy" format of date
DateTime dateTime2 = DateTime.parse(textBox2.Text); //Second input of date value

//Comparing them
if (dateTime1.Year != dateTime.Year)
{
    //Some action
}
 
Share this answer
 
Comments
falque 31-Dec-12 8:15am    
Thanks for the concern.Its working fine now but there is one more requirement as when the user put the value in the second text box it automatically compares the value of year in both the text boxes if its similar its ok else produce an error message showing the years are not mathching. Any help would be appreciated.Thanking You
you can use like this for getting year from textbox data & then can compare easily...

C#
DateTime dt = "xx/xx/xxxx";//your date Convert.ToDate(TextBox1.Text)
int year1 = dt.Year;


Thanks
Developers Blog[^]
Hemant Singh Rautela
 
Share this answer
 
Comments
falque 31-Dec-12 2:51am    
Thanks for great help but whatever date format i m putting its giving an error message showing "String was not recognized as a valid DateTime".Please provide the solution.Thanking you
Hemant Singh Rautela 31-Dec-12 5:20am    
you can use CalenderExtender control of ajaxtoolkit for picking date, Then it works perfectly. Default datetime format is (mm/dd/yyyy)


& if you like solution then vote plz...

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