Posts Tagged date

Converting dd-MM-yyyy HH:mm type of string expressions to date

Posted by Ozer Senturk on Tuesday, 9 March, 2010
/// <summary>
/// Converts dd-MM-yyyy HH:mm type of string expressions to date
/// </summary>
/// <param name="s">The string to parse</param>
/// <returns>The result date</returns>
public static DateTime? parseDateWithTime(String s)
{
    String pattern = @"(?<Day>\d{1,2})-(?<Month>\d{1,2})-(?<Year>\d{4}) (?<Hour>\d{2}):(?<Minute>\d{2})";
    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),
                            Int32.Parse(DateMatch.Groups["Hour"].Value),
                            Int32.Parse(DateMatch.Groups["Minute"].Value),
                            0);

    return null;
}

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;
}