How to check if a string is in a valid DateTime format?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello, guys,

what is the easiest way to check if a string is in a valid DateTime format
or not in C#.net?

Thanks.
 
Use Convert.ToDateTime(yourstring) inside a try/catch. If it comes inside the catch, then the passed string is probably invalid.

Sonu Kapoor [MVP]
 
Andrew said:
what is the easiest way to check if a string is in a valid DateTime format
or not in C#.net?

'DateTime.Parse'/'DateTime.ParseExact' + 'try...catch' in .NET 1.*,
'DateTime.TryParse' in .NET 2.0.
 
Andrew,

I believe that would be DateTime.TryParse (if you want just to check)or
DateTime.Parse.
 
Back
Top