Check Date Format

G

Guest

Hi all,
i have to check if a value is a valid date format depending on a "format
string" the user specified. For example:

Format String: "dd/mm/yyyy"
Date Value: "01/12/2006"
Valid: Yes!

Format String: "yyyymmdd"
Date Value: "01/01/2006"
Valid: No!

Format String: "dd.mm.yyyy"
Date Value: "12/31/2006"
Valid: No!

Format String: "mm/dd/yyyy"
Date Value: "12/31/2006"
Valid: Yes!

I always have the "dd", "mm" and "yyyy" values available, i also have the
delimiter between them.

So what is the best way to check for validation?

Thanks in advance.
Thomas
 
J

Jon Skeet [C# MVP]

Thomas said:
i have to check if a value is a valid date format depending on a "format
string" the user specified. For example:

Format String: "dd/mm/yyyy"
Date Value: "01/12/2006"
Valid: Yes!

Format String: "yyyymmdd"
Date Value: "01/01/2006"
Valid: No!

Format String: "dd.mm.yyyy"
Date Value: "12/31/2006"
Valid: No!

Format String: "mm/dd/yyyy"
Date Value: "12/31/2006"
Valid: Yes!

I always have the "dd", "mm" and "yyyy" values available, i also have the
delimiter between them.

So what is the best way to check for validation?

Have a look at DateTime.ParseExact, and DateTime.TryParseExact in .NET
2.0.
 

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