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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: