I would like to easily validate dates in this format and only this format. Any otehr format should be considered invalid.
views:
49answers:
3
+1
A:
I don't know if regex is possible in VB.NET, but then it'll be relatively easy:
/[0-9]{2}-[a-zA-Z]{3}-[0-9]{4}/
This might be a bit language specific when it comes down to escaping characters and matching certain groups.
wvd
2010-04-24 07:03:58
While regex is surely a valid approach your sample would allow `99-fOO-0000` which isn't a valid date.
Filburt
2010-04-24 09:07:44
This is true -- but I shouldn't have answered this question since I also kind of knew there were API functions who would be much better at this.
wvd
2010-04-24 09:10:19
Truth is I'm a total looser with regex beyond your sample so I hoped you would take it a step further and give me something to go "Ah!".
Filburt
2010-04-24 09:21:06
+3
A:
You use DateTime.ParseExact
or DateTime.TryParseExact
. You pass through the exact format string.
In your case the format string would be d-MMM-yyyy
(see here) and can be used as follows:
string dateString = "31-JUL-2010";
string format = "d-MMM-yyyy";
DateTime result = DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture);
Oded
2010-04-24 07:04:37
+2
A:
Dim DateToTest As String = "01-Apr-1964"
Dim ResultDate As Date
Date.TryParseExact(DateToTest, "dd-MMM-yyyy", Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.AllowWhiteSpaces, ResultDate)
Velika
2010-04-24 07:16:42