Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4240588
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:11:06+00:00 2026-05-21T03:11:06+00:00

Apologies if this has been asked before. I have some data which I need

  • 0

Apologies if this has been asked before. I have some data which I need to store as strings, some of that data being dates. The data starts off as strings like “01/02/10” (uk format). Now, later on, this data is parsed and, depending upon what does the parsing, the results are different (01-Feb-10 vs. 02-Jan-10 for example). Given that the data starts off as strings, before I stored it I would like to say, “if this looks like a date, format it as dd-mmm-yy”.

The trouble being that many things look like a date to the DateTime.Parse() function.

So, I applied some rules and only accept “reasonable” date formats for my checks, and wrote an IsDate() function. I’m seeking suggestions about how to do this because, while it works, my solution seems very clunky.

The whole reason why I did this rather than go down the usual DateTime.TryParse routine is clear if you have ever started throwing random strings at it (like “3/4” and “6.12”).

Here’s what I have so far:

class Program
{
  static void Main(string[] args)
  {
     Debug.Assert(IsDate(6.12) == false);
     Debug.Assert(IsDate("3/4") == false);
     Debug.Assert(IsDate(010210) == false);
     Debug.Assert(IsDate("010210") == false);
     Debug.Assert(IsDate("12-jan-2000") == true);
     Debug.Assert(IsDate("12-12-20") == true);
     Debug.Assert(IsDate("1/1/34") == true);
     Debug.Assert(IsDate("09/30/20") == false);
     Debug.Assert(IsDate(DateTime.Now) == true);
  }

  static Boolean IsDate(Object value)
  {
     DateTimeFormatInfo DateTimeFormatGB = new CultureInfo("en-GB").DateTimeFormat; // new CultureInfo("en-US").DateTimeFormat;
     return IsDate(value, DateTimeFormatGB);
  }

  static private List<String> AcceptableDateFormats = new List<String>(72);
  static Boolean IsDate(Object value, DateTimeFormatInfo formatInfo)
  {
     if (AcceptableDateFormats.Count == 0)
     {
        foreach (var dateFormat in new[] { "d", "dd" })
        {
           foreach (var monthFormat in new[] { "M", "MM", "MMM" })
           {
              foreach (var yearFormat in new[] { "yy", "yyyy" })
              {
                 foreach (var separator in new[] { "-", "/" }) // formatInfo.DateSeparator ?
                 {
                    String shortDateFormat;
                    shortDateFormat = dateFormat + separator + monthFormat + separator + yearFormat;
                    AcceptableDateFormats.Add(shortDateFormat);
                    AcceptableDateFormats.Add(shortDateFormat + " " + "HH:mm"); // formatInfo.TimeSeparator
                    AcceptableDateFormats.Add(shortDateFormat + " " + "HH:mm:ss");
                 }
              }
           }
        }
     }

     String sValue = value.ToString().Trim();
     DateTime unused;

     foreach (String format in AcceptableDateFormats)
     {
        if (DateTime.TryParseExact(sValue, format, formatInfo, DateTimeStyles.None, out unused) == true) return true;
     }

     return false;
  }
}

I didn’t use the date/time separators from the culture information because I wanted to accept both a “/” and a “-“. I guess I could have used the time one though, as that’s unlikely to change (for me).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T03:11:07+00:00Added an answer on May 21, 2026 at 3:11 am

    in the end, I went with a version of the following:

      static private List<String> AcceptableDateFormats = new List<String>(180);
      static Boolean IsDate(Object value, DateTimeFormatInfo formatInfo)
      {
         if (AcceptableDateFormats.Count == 0)
         {
            foreach (var dateFormat in new[] { "d", "dd" })
            {
               foreach (var monthFormat in new[] { "M", "MM", "MMM" })
               {
                  foreach (var yearFormat in new[] { "yy", "yyyy" })
                  {
                     foreach (var separator in new[] { "-", "/", formatInfo.DateSeparator  })
                     {
                        String shortDateFormat;
                        shortDateFormat = dateFormat + separator + monthFormat + separator + yearFormat;
                        AcceptableDateFormats.Add(shortDateFormat);
                        AcceptableDateFormats.Add(shortDateFormat + " " + "HH:mm");
                        AcceptableDateFormats.Add(shortDateFormat + " " + "HH:mm:ss");
                        AcceptableDateFormats.Add(shortDateFormat + " " + "HH" + formatInfo.TimeSeparator + "mm");
                        AcceptableDateFormats.Add(shortDateFormat + " " + "HH" + formatInfo.TimeSeparator + "mm" + formatInfo.TimeSeparator + "ss");
                     }
                  }
               }
            }
            AcceptableDateFormats = AcceptableDateFormats.Distinct().ToList();
         }
    
         DateTime unused;
         return DateTime.TryParseExact(value.ToString(), AcceptableDateFormats.ToArray(), formatInfo, DateTimeStyles.AllowWhiteSpaces, out unused);
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Apologies if this question has been asked before. I'm hoping that someone can step
I'm sure this has been asked before in some other way that I just
Apologies if this has been asked before (I couldn't find the answer anywhere), but
Apologies if this has been asked before. I searched and couldn't find it. In
My apologies if this has been asked before, I wasnt quite sure if this
First off, apologies if this question has been asked before but I couldn't find
This has almost certainly been asked here before, so apologies if it's a duplicate.
I apologize if this has been asked before. I searched but did not find
I apologise if this has been asked before but I can't find the info
If this has been asked before, I apologize but this is kinda of a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.