Change date format using .NET Regular Expressions

You can use Regex.Replace to change the format of a date string. For example if your input string is YYYY/MM/DD but you need to change it to YYYY-MM-DD, you can use this small method:

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

Regular expressions are unreadable as usual, but the trick here lies in the way the year, month and day parts are grabbed and stored in “variables” to be used in the replacement part of the Regex.Replace:

  • Pattern: (?\\d{2,4}) grabs 2-4 digits and places them in a variable called “year”. The same goes for month and day.
  • Replacement: ${year} places the value grabbed from the input and places it into the output of the replace function.

With this in mind we can create lots of different search/replace patters:

  • Input: DD/MM/YYYY Output: YYYY-MM-DD
    Pattern: \\b(?<day>\\d{1,2})/(??<month>\\d{1,2})/(??<year>\\d{2,4})\\b
    Replacement: ${year}-${month}-${day}
  • Input: YYYY-MM-DD Output: MM/DD/YYYY
    Patern: \\b(??<year>\\d{2,4})-(??<month>\\d{1,2})-(??<day>\\d{1,2})\\b
    Replacement: ${month}/${day}/${year}

Would you like to change multiple dates from your input? Add RegexOptions.Multiline to the options:

public static string ReformatDate(string input) 
{
   try {
      return Regex.Replace(input, 
            "\\b(?<year>\\d{2,4})/(?<month>\\d{1,2})/(?<day>\\d{1,2})\\b",
            "${year}-${month}-${day}", 
            RegexOptions.Multiline | RegexOptions.IgnoreCase,
            TimeSpan.FromMilliseconds(1000));
   }         
   catch (RegexMatchTimeoutException) {
      return input;
   }
}

Or how about changing dates from within an XML document?

<item>
 <start_date>2019/06/11</start_date>
 <expiration_date>2019/06/17</g:expiration_date>
</item>

Add the < > to the pattern and replacement variables:

public static string ReformatDate(string input) 
{
   try {
      return Regex.Replace(input, 
            ">\\b(?<year>\\d{2,4})/(?<month>\\d{1,2})/(?<day>\\d{1,2})\\b<", 
            ">${year}-${month}-${day}<", 
            RegexOptions.Multiline | RegexOptions.IgnoreCase,
            TimeSpan.FromMilliseconds(1000));
   }         
   catch (RegexMatchTimeoutException) {
      return input;
   }
}

MORE TO READ:

 

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in .net, .NET Core, c#, General .NET and tagged , , . Bookmark the permalink.

1 Response to Change date format using .NET Regular Expressions

  1. You can add in-line comments to regular expressions in .NET.

    See here for an example:

    https://www.oreilly.com/library/view/regular-expressions-cookbook/9781449327453/ch02s18.html

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.