Solved

Validating date in MMDDYYYY format in C#

Posted on 2006-10-24
2
1,615 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

756 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