I have a datetime string.
string strDate = "20140424_18255375";
How to verify the datetime is having in this format YYYYMMDD_HHmmssff
I tried:
bool isTrue = DateTime.TryParseExact(strDate, "YYYYMMDD_HHmmssff", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt);
Please help if there is a better way to verify the datetimes with RegEx or any normal way.
Using TryParseExact
is the right way to go about it, but you need to use the right format specifiers. In this case, I think you want:
bool valid = DateTime.TryParseExact(strDate, "yyyyMMdd_HHmmssff",
CultureInfo.InvariantCulture, DateTimeStyles.None, out dt);
Note the use of yyyy
instead of YYYY
and dd
instead of DD
. Format specifiers are case-sensitive.
See more on this question at Stackoverflow