Link to home
Start Free TrialLog in
Avatar of pdurbha
pdurbha

asked on

Validating date in MMDDYYYY format in C#

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
Avatar of WinterMuteUK
WinterMuteUK
Flag of United Kingdom of Great Britain and Northern Ireland image

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

Cheers!
Wint
ASKER CERTIFIED SOLUTION
Avatar of e1v
e1v

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial