How to check if date is in correct format i.e. yyyy-mmm-dd?
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.
- Check that the format is correct with Regular Expressions.
- Check that the data is correct by casting it to a date and catching any exceptions.
Admin
Updated on July 18, 2022Comments
-
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 like2015-11-19
. What do I do? -
Admin over 8 yearsI want the date to be like this: 2015-Nov-19
-
Paurian over 8 yearsActually - 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 over 8 yearsYou 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 nommm
the parse treats it as an opaque literal with no bearing on the date produced, and so parses2015-11-19
as meaning2015-01-19
(there's no value obtained for month, so it defaults to1
). -
Morpheus over 8 years
yay
-> party like it's 1999 -
Soner Gönül over 8 years@JonHanna Exactly. Even if
mmm
were a valid specifier, since it points minutes, month value would be1
by default as you said. Why you said I'm better the first time by the way? My first revision does not contains parsing the2015-01-19
case :) -
Jon Hanna over 8 yearsExactly. Your revision contains the parsing for the
2015-11-19
case, which it should be blocking. -
Don over 8 yearsFixed typo, I blame my phone ☺
-
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 over 8 yearsI think you have misunderstood his question, I believe he doesn't want that format to be supported.