Export (0) Print
Expand All

How to: Change Date Formats

The following example uses the Regex.Replace method to replace dates of the form mm/dd/yy with dates of the form dd-mm-yy. The Replace(String, String, String) function is one of several static functions that enable you to use regular expression operations without creating an explicit regular expression object. This example demonstrates the use of named backreferences within the replacement pattern for Replace(String, String, String). Here, the replacement expression ${day} inserts the substring captured by the group (?<day>…).

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

public class Example
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
      string dateString = DateTime.Today.ToString("d",
      string resultString = MDYToDMY(dateString);
      outputBlock.Text += String.Format("Converted {0} to {1}.", dateString, resultString) + "\n";

   static string MDYToDMY(string input)
      return Regex.Replace(input,

Community Additions

© 2014 Microsoft