Solved

Validating date in MMDDYYYY format in C#

Posted on 2006-10-24
2
1,619 Views
Last Modified: 2010-05-18
Hi,

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)

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

                        DateTime dt = DateTime.ParseExact(date,date_format,dtfi);
                                          }
                  catch(Exception)
                  {
                        
                                                   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........

Thanks
Praveen
0
Comment
Question by:pdurbha
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 9

Expert Comment

by:WinterMuteUK
ID: 17801540
Hullo,
What is the exact string you are using for the date_format string?

Cheers!
Wint
0
 
LVL 6

Accepted Solution

by:
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);
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Adding items to a C# list incrementally 5 82
How to escape alias names for Oracle query 1 41
Stop Git from being my repository 1 50
asp.net repeater server posting 5 38
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

751 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