Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

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,

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

© 2014 Microsoft. All rights reserved.