7

I want to check if a date has a correct format. There is many possibilities of correct dates like:

  • 02.08.2010
  • 2.8.2010
  • 02.8.2010 02.08
  • 02.August
  • ...

I can test each on with code like this:

if (DateTime.TryParse(DateTime.ParseExact(date, "dd.M.", 
                              new CultureInfo("sl-SI")).ToString(), out dt))

But then I can have 40 if statements. Is it possible to check all dates with one if statement or one loop?

Update:

Based on the answers so far, I am testing this code, but I have one more problem. What if I have just 9.2 not 9.2.2010 then this code will not work:

CultureInfo ci = CultureInfo.GetCultureInfo("sl-SI");
string[] fmts = ci.DateTimeFormat.GetAllDateTimePatterns();

if (DateTime.TryParseExact(date, fmts, ci, DateTimeStyles.AssumeLocal, out dt))
{
    DateTime = Convert.ToDateTime(date);
    Check = true;
}

Must I manually add this times or what can I do?

Kev
  • 118,037
  • 53
  • 300
  • 385
senzacionale
  • 20,448
  • 67
  • 204
  • 316
  • How come you accept every possible date format as valid input? – Captain Sensible Apr 27 '10 at 17:24
  • please add updates to your question or use the comments feature. Also please don't use answers to say thanks, use a comment or better, upvote and mark as accepted: http://blog.stackoverflow.com/2011/01/how-to-say-thanks-in-an-answer/ – Kev Jan 29 '11 at 15:36
  • Might want to see this as well [robust-datetime-parser-library-for-net](http://stackoverflow.com/questions/7297622/robust-datetime-parser-library-for-net) – nawfal Jan 30 '14 at 08:05

3 Answers3

9

Yes ParseExact can take a list of formats to check against.

var formats = new[] { "M.d.yyyy", "dd.MM.yyyy" };
var dateValue = DateTime.ParseExact(
    dateString, formats, new CultureInfo("sl-SI"), DateTimeStyles.None);
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
9

You can use something like the following, but be aware that more than one format might be able to parse the same date. For example 10/11/12 can be parsed as yy/MM/dd or MM/dd/yy, which are both valid US date formats. MM/dd/yy is more common, so it appears first in the list and is the one returned by the code below (if you use it with a US culture instead of the culture in the example).

string testValue = "10.11.12";

DateTime result;
CultureInfo ci = CultureInfo.GetCultureInfo("sl-SI");
string[] fmts = ci.DateTimeFormat.GetAllDateTimePatterns();
Console.WriteLine(String.Join("\r\n", fmts));
if (DateTime.TryParseExact(testValue, fmts, ci,
   DateTimeStyles.AssumeLocal, out result))
{
   Console.WriteLine(result.ToLongDateString());
}
BlueMonkMN
  • 25,079
  • 9
  • 80
  • 146
  • 3
    In the part of the world where I live, humans would parse it as "dd.mm.yy". Taking that into account, the whole question makes little sense, because it's prone to misinterprations. – Erich Kitzmueller Feb 24 '10 at 13:46
0

You can actually try all date formats in every possible pattern, then use ParseExact to format the value you want

public string GetDateFormat(string input)
    {
        string[] formats = {"M/d/yyyy", "MM/dd/yyyy",
                        "d/M/yyyy", "dd/MM/yyyy",
                        "yyyy/M/d", "yyyy/MM/dd",
                        "M-d-yyyy", "MM-dd-yyyy",
                        "d-M-yyyy", "dd-MM-yyyy",
                        "yyyy-M-d", "yyyy-MM-dd",
                        "M.d.yyyy", "MM.dd.yyyy",
                        "d.M.yyyy", "dd.MM.yyyy",
                        "yyyy.M.d", "yyyy.MM.dd",
                        "M,d,yyyy", "MM,dd,yyyy",
                        "d,M,yyyy", "dd,MM,yyyy",
                        "yyyy,M,d", "yyyy,MM,dd",
                        "M d yyyy", "MM dd yyyy",
                        "d M yyyy", "dd MM yyyy",
                        "yyyy M d", "yyyy MM dd",
                        "d-MMM-yyyy", "d/MMM/yyyy", "d MMM yyyy", "d.MMM.yyyy",
                        "d-MMM-y", "d/MMM/y", "d MMM y", "d.MMM.y",
                        "dd-MMM-yyyy", "dd/MMM/yyyy", "dd MMM yyyy", "dd.MMM.yyyy",
                        "MMM/dd/yyyy", "MMM-dd-yyyy", "MMM dd yyyy", "MMM.dd.yyyy", "MMM.dd.yyyy"
                       };

        DateTime dateValue;
        foreach (string dateStringFormat in formats)
        {
            if (DateTime.TryParseExact(input, dateStringFormat,
                                       CultureInfo.InvariantCulture,
                                       DateTimeStyles.None,
                                       out dateValue))
            {
                Console.WriteLine(dateStringFormat);

                return dateStringFormat;
            }
        }

        return null;
    }
samheihey
  • 169
  • 1
  • 3
  • 13