We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now


Validating date in MMDDYYYY format in C#

pdurbha asked
Medium Priority
Last Modified: 2010-05-18

I am writing a small piece of code that validates a given date against the MMDDYYYY format

Here is my code:

//Validate string date with string date_format MMDDYYYY

public bool validate_datet(string date, string date_format)

                        DateTimeFormatInfo dtfi = new DateTimeFormatInfo();
                        dtfi.LongDatePattern = date_format;

                        DateTime dt = DateTime.ParseExact(date,date_format,dtfi);
                                                   return false;
            return true;

This function returns false even when I pass in a valid date in the MMDDYYYY format such as 06251966. Could anybody tell me what it is that  I am doing wrong here..anybody........

Watch Question

What is the exact string you are using for the date_format string?

validate_datet("06251966", "MMDDYYY") will fail because the date format string is case sensitive, you'll have to use "MMddyyyy"

You do not need to use DateTimeFormatInfo either, you could just use DateTime.ParseExact(date,date_format, CultureInfo.InvariantCulture);

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.