UK to MySQL Date in C#

A C# snippet that will allow a function to be written where a string is passed in, containing a date in the format dd/mm/yyyy, and have that replaced by yyyy-mm-dd:

// Matches 2 digits forward slash 2 digits forward slash 4 digits e.g. 22/02/2013
Match m = Regex.Match(input, "\d{2}/\d{2}/\d{4}");
// could use @"d{2}/d{2}/d{4}" instead of the double backslash escape
if (m.Index > 0)
{
    string dd = input.Substring(m.Index, 2);
    string mm = input.Substring(m.Index + 3, 2);
    string yyyy = input.Substring(m.Index + 6, 4);

    input = input.Replace(dd + "/" + mm + "/" + yyyy, yyyy + "-" + mm + "-" + dd);
}

Leave a Reply

Your email address will not be published. Required fields are marked *

*