I have a string 01-Jan-2014 00:00:00 and I intend to shorten the year into 2 characters.
my code:
DateTime dtParsedDate = new DateTime();
string strInput = "01-Jan-2014 00:00:00";
Regex regDate = new Regex(@"\d{2}-\w{3}-\d{4}");
// parse into datetime object
dtParsedDate = DateTime.ParseExact(regDate.Match(strInput).Value, "dd-MMM-yyyy", CultureInfo.InvariantCulture);
// replace the string with new format
regDate.Replace(arrData[iCol], dtParsedDate.ToString("dd-MMM-yy"));
I've verified that the string is matched correctly using regex.
"01-Jan-2014" did not get replaced into "01-Jan-14". What is wrong with my code?
Regex.Replacereturns a new string, it does not mutate the string you pass in to it (because strings are immutable).