public static DateTime ParseDate(string value)
{
DateTime date = new DateTime();
if (value.Length <= 7) // 1.1.98, 21.3.98, 1.12.98,
DateTime.TryParseExact(value, "d.M.yy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AssumeLocal, out date);
else if (value.Length == 8 && value[5] == '.') // 21.01.98
DateTime.TryParseExact(value, "dd.MM.yy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AssumeLocal, out date);
else if (value.Length <= 9) // 1.1.1998, 21.1.1998, 1.12.1998
DateTime.TryParseExact(value, "d.M.yyyy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AssumeLocal, out date);
else if (value.Length == 10) // 21.01.1998
DateTime.TryParseExact(value, "dd.MM.yyyy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AssumeLocal, out date);
return date;
}
public static DateTime? ExtractDate(string text)
{
DateTime? ret = null;
Regex regex = new Regex(@"\d{1,2}\.\d{1,2}\.\d{2,4}");
MatchCollection matches = regex.Matches(text);
foreach (Match match in matches)
{
DateTime date = ParseDate(match.Value);
if (ret == null || ret < date)
ret = date;
}
return ret;
}