You can use the Regex.Replace method to replace one date format to another date format in the entire string. 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 ReplaceDateString(string strIn) { return System.Text.RegularExpressions.Regex.Replace(strIn, "\\b(?<month>\\d{1,2})/(?<day>\\d{1,2})/(?<year>\\d{2,4})\\b", "${day}-${month}-${year}", System.Text.RegularExpressions.RegexOptions.None); }
In the following code, ReplaceDateString will replace date string from mm/dd/yyyy format to mm-dd-yyyy. You can use the ReplaceDateString as:
string str = ReplaceDateString(" Todays date is 10/26/2012");
from the above code you can observe that the value of str will be “Todays date is 10-26-2012”