Date Validation - Is this a real date?

Posted on 2009-04-25
Last Modified: 2013-12-17
I need to validate a date provided by a user.  I'm currently using the attached snippet, however, it returns a true when validating 2/31/1999.  There has to be a better way to do this.
private bool IsDate(string dt)


	DateTime testDate;


	if (!DateTime.TryParse(dt, out testDate))


		return true;



	return false;


Open in new window

Question by:andy_ee
    LVL 3

    Accepted Solution

    Try the following method


    string dateString, format;  
    DateTime result;
    CultureInfo provider = CultureInfo.InvariantCulture;
    // Parse date-only value with invariant culture.
    dateString = "06/15/2008";
    format = "MM/dd/yyyy";
    try {
       result = DateTime.ParseExact(dateString, format, provider);

    Open in new window

    LVL 23

    Expert Comment

    Its accepting US formatted dates "MM/dd/yyyy".

    Your probably wanting international dates "dd/MM/yyyy"

    Changing Aleksei_Malkov suggestion to use  "dd/MM/yyyy" will probably get you what you want.

    p.s. it indicates that your current culture may be set to US

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now