Validating date in MMDDYYYY format in C#

Posted on 2006-10-24
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........

Question by:pdurbha

Expert Comment

ID: 17801540
What is the exact string you are using for the date_format string?


Accepted Solution

e1v earned 500 total points
ID: 17801758
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);

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Chat Room 1 43
C# GridRow get Old/New Value 1 55
Different Delete Messages 7 37
Stream.BeginRead and Stream.EndRead in .NET Core 5 38
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 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

25 Experts available now in Live!

Get 1:1 Help Now