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

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.
 
S

Sonu Kapoor

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

Sonu Kapoor [MVP]
 
H

Herfried K. Wagner [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.
 
S

Stoitcho Goutsev \(100\)

Andrew,

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top