Solved

Validating date in MMDDYYYY format in C#

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

18 Experts available now in Live!

Get 1:1 Help Now