My Technical Notes

Monday, 18 March 2013

Regex for determining if a Date string is in the correct format

Assuming you wish to check if a string is in the format: day/month/year, I would use the following Regular expression:

Regex.IsMatch(str, @"\d\d/\d\d/\d\d\d\d");

Thereafter, to parse it into the correct DateTime object, we can use the DateTime.ParseExact

DateTime.ParseExact(str, "dd/MM/yyyy", new CultureInfo("en-GB"));

No comments: