Click here to Skip to main content
15,882,152 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In my code i want to validate input date is not greater than current date and input date is correct date.

What I have tried:

i don't have any idea how to do please help
Posted
Updated 6-Jul-17 1:45am

Don;t use Parse: use TryParse or TryParseExact. Parse throws an exception is the user makes a mistake - which they do, all too frequently - the TryParse versions don't.
C#
DateTime dt;
if (!DateTime.TryParse(inputString, out dt))
   {
   // Report problem to user
   ... 
   return;
   }
if (dt > DateTime.Now.Date)
   {
   // In the future
   ...
   return;
   }
 
Share this answer
 
C#
string date = "2017-08-02";
DateTime myDate = DateTime.Parse(date);
Console.WriteLine("my date is: " + myDate);
		
if (myDate > DateTime.Now)
{
	Console.WriteLine("my date > " + DateTime.Now);
}
else
{
	Console.WriteLine("my date <= " + DateTime.Now);
}
Try it out here: Home | .NET Fiddle[^]
Also see: [datetime-parse]
 
Share this answer
 
v3

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