How to check if date is in correct format i.e. yyyy-mmm-dd?

23,501

Solution 1

Use DateTime.TryParseExact

DateTime dt;
string[] formats = { "yyyy-MMM-dd"};
if (!DateTime.TryParseExact(dateValue, formats, 
                System.Globalization.CultureInfo.InvariantCulture,
                DateTimeStyles.None, out dt))
{
    //your condition fail code goes here
    return false;
}
else
{
    //success code
}

Solution 2

You need to use MMM specifier instead of mmm with a english-based culture like InvariantCulture. There is no mmm as a custom date and time specifier.

And if you wanna parse multiple formats, DateTime.TryParseExact has an overload that takes string[] as a paramter so you can supply multiple formats.

string s = "2015-Nov-19"; // or 2015-11-19
DateTime dt;
string[] formats = {"yyyy-MMM-dd", "yyyy-MM-dd"};
if(DateTime.TryParseExact(s, formats, CultureInfo.InvariantCulture,
                          DateTimeStyles.None, out dt))
{
    // Successfully parse
}

I used MM specifier in second format but if your single digit months does not have leading zero, you need to use M specifier instead.

Solution 3

Use DateTime.TryParseExact and specify the expected format string appropriately.

Solution 4

You could use a regular expression. For dates, it can be hairy if you want to account for the number of days any month is allowed and for leap-years. Perhaps a two-step approach would be easier.

  1. Check that the format is correct with Regular Expressions.
  2. Check that the data is correct by casting it to a date and catching any exceptions.
Share:
23,501
Admin
Author by

Admin

Updated on July 18, 2022

Comments

  • Admin
    Admin almost 2 years

    I'm receiving data from a service call and want to make sure that the string is in correct date format like:

    2015-Nov-19
    

    I have tried to use parse function with the format yyyy-mmm-dd, but it is also allowing dates like 2015-11-19. What do I do?

  • Admin
    Admin over 8 years
    I want the date to be like this: 2015-Nov-19
  • Paurian
    Paurian over 8 years
    Actually - I like @Jon 's answer much better. I didn't know about the "Exact" variant. This is one of the reasons I have a sincere appreciation for StackOverflow - I learn something new even when trying to help others.
  • Jon Hanna
    Jon Hanna over 8 years
    You were better the first time. @TimSchmelter notes that the OP sasid it accepted dates like 2015-11-19, but that was a problem with the original, since there is no mmm the parse treats it as an opaque literal with no bearing on the date produced, and so parses 2015-11-19 as meaning 2015-01-19 (there's no value obtained for month, so it defaults to 1).
  • Morpheus
    Morpheus over 8 years
    yay -> party like it's 1999
  • Soner Gönül
    Soner Gönül over 8 years
    @JonHanna Exactly. Even if mmm were a valid specifier, since it points minutes, month value would be 1 by default as you said. Why you said I'm better the first time by the way? My first revision does not contains parsing the 2015-01-19 case :)
  • Jon Hanna
    Jon Hanna over 8 years
    Exactly. Your revision contains the parsing for the 2015-11-19 case, which it should be blocking.
  • Don
    Don over 8 years
    Fixed typo, I blame my phone ☺
  • Soner Gönül
    Soner Gönül over 8 years
    @TahreemIqbal Why you select this as an answer o.O? You said but it is also allowing dates like 2015-11-19 but this answer does not handle that.
  • Don
    Don over 8 years
    I think you have misunderstood his question, I believe he doesn't want that format to be supported.