Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
CAN WE USE IF CONDITION WITH DATES..

can i implement the fallowing functionality with dates..





C#
if (TextBox1.Text<01/01/2001)

{
lblres.Text="value should be more than 01/01/2001";
}


i know the syntax is wrong.. what would be the correct syntax..
Posted

Have a look at DateTime.Parse Method[^]. You can use that for your input.
 
Share this answer
 
Comments
Wonde Tadesse 3-Dec-11 15:19pm    
5+
Wendelius 3-Dec-11 17:30pm    
Thank you :)
Monjurul Habib 3-Dec-11 15:50pm    
nice link but TryParse is better.5*
Wendelius 3-Dec-11 17:31pm    
Thank you, depending on the error handle politics, TryParse may suit better.
LanFanNinja 3-Dec-11 18:20pm    
+5 agreed!
Use DateTime.TryParse[^] to avoid unnecessary exception.
 
Share this answer
 
Comments
Monjurul Habib 3-Dec-11 15:51pm    
nice link. 5*
Wonde Tadesse 3-Dec-11 15:55pm    
Thanks
thatraja 3-Dec-11 21:54pm    
5!
Wonde Tadesse 3-Dec-11 21:55pm    
Thanks
This is worse than pulling teeth!
Spend some a lot of time learning about the DateTime[^] structure, and use it. You cannot use comparison operators on dates stored as strings, apart from simple string equality, and even then you can get the wrong answer.
 
Share this answer
 
Comments
Wonde Tadesse 3-Dec-11 15:57pm    
5+
thatraja 3-Dec-11 21:54pm    
5!
Try following:

C#
if(!String.IsNullOrEmpty(TextBox1.Text))
{
  if (Convert.ToDateTime(TextBox1.Text) < Convert.ToDateTime("01/01/2001"))
  {
    lblres.Text="value should be more than 01/01/2001";
  }
}

For more advanced you can follow the links:
http://msdn.microsoft.com/en-us/library/ch92fbc1.aspx[^]
http://msdn.microsoft.com/en-us/library/9h21f14e.aspx[^]

Let me know if you need further information.

Enjoy :)
 
Share this answer
 
v2
Comments
Wonde Tadesse 3-Dec-11 15:56pm    
5+
Monjurul Habib 3-Dec-11 16:09pm    
thank you
Philippe Mori 3-Dec-11 17:48pm    
It does not make much sense to use Convert.ToDateTime to convert the hard-coded string. Create a DateTime object with the appropriate date.
thatraja 3-Dec-11 21:54pm    
Only 4! yes agree with Philippe
Monjurul Habib 26-Dec-11 23:52pm    
thanks

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