Export (0) Print
Expand All
1 out of 2 rated this helpful - Rate this topic

Example: Changing Date Formats

The following code example uses the Regex.Replace method to replace dates that have the form mm/dd/yy with dates that have the form dd-mm-yy.

static string MDYToDMY(string input) 
{
   try {
      return Regex.Replace(input, 
            "\\b(?<month>\\d{1,2})/(?<day>\\d{1,2})/(?<year>\\d{2,4})\\b",
            "${day}-${month}-${year}", RegexOptions.None,
            TimeSpan.FromMilliseconds(150));
   }         
   catch (RegexMatchTimeoutException) {
      return input;
   }
}

The following code shows how the MDYToDMY method can be called in an application.

using System;
using System.Globalization;
using System.Text.RegularExpressions;

public class Class1
{
   public static void Main()
   {
      string dateString = DateTime.Today.ToString("d", 
                                        DateTimeFormatInfo.InvariantInfo);
      string resultString = MDYToDMY(dateString);
      Console.WriteLine("Converted {0} to {1}.", dateString, resultString);
   }

   static string MDYToDMY(string input) 
   {
      try {
         return Regex.Replace(input, 
               "\\b(?<month>\\d{1,2})/(?<day>\\d{1,2})/(?<year>\\d{2,4})\\b",
               "${day}-${month}-${year}", RegexOptions.None,
               TimeSpan.FromMilliseconds(150));
      }         
      catch (RegexMatchTimeoutException) {
         return input;
      }
   }

}
// The example displays the following output to the console if run on 8/21/2007: 
//      Converted 08/21/2007 to 21-08-2007.

The regular expression pattern \b(?<month>\d{1,2})/(?<day>\d{1,2})/(?<year>\d{2,4})\b is interpreted as shown in the following table.

Pattern

Description

\b

Begin the match at a word boundary.

(?<month>\d{1,2})

Match one or two decimal digits. This is the month captured group.

/

Match the slash mark.

(?<day>\d{1,2})

Match one or two decimal digits. This is the day captured group.

/

Match the slash mark.

(?<year>\d{2,4})

Match from two to four decimal digits. This is the year captured group.

\b

End the match at a word boundary.

The pattern ${day}-${month}-${year} defines the replacement string as shown in the following table.

Pattern

Description

$(day)

Add the string captured by the day capturing group.

-

Add a hyphen.

$(month)

Add the string captured by the month capturing group.

-

Add a hyphen.

$(year)

Add the string captured by the year capturing group.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.