Posts Tagged dd-MM-yyyy

Converting dd-MM-yyyy type of string expressions to date

Posted by Ozer Senturk on Tuesday, 9 March, 2010
/// <summary>
/// Converts dd-MM-yyyy type of string expressions to date
/// </summary>
/// <param name="s">The string to parse</param>
/// <returns>The result date</returns>
public static DateTime? parseDate(String s)
{
    String pattern = @"(?<Day>\d{1,2})-(?<Month>\d{1,2})-(?<Year>\d{4})";
    Match DateMatch = Regex.Match(s, pattern);
    if (DateMatch.Success)
        return new DateTime(Int32.Parse(DateMatch.Groups["Year"].Value),
                            Int32.Parse(DateMatch.Groups["Month"].Value),
                            Int32.Parse(DateMatch.Groups["Day"].Value));

    return null;
}