It is a breaking change.
See: Formatting and Parsing Time Intervals in the .NET Framework 4
In the .NET Framework 3.5 and earlier versions, TimeSpan does not
implement IFormattable
, nor does it support format strings.
Therefore, the “r” format string is ignored, and the parameterless
TimeSpan.ToString method is called. In the .NET Framework 4, on the
other hand, TimeSpan.ToString(String, IFormatProvider) is called and
passed the unsupported format string, which causes the exception.
Just to expand on the answer, your original code in .Net framework 2.0 would not throw an exception but it will not give you the desired output, (minutes:seconds). Since the parameter less constructor would be call for TimeSpan
, ignoring the format specified in String.Format
. But, with .Net framework 4.0 or hgiher , since TimeSpan
implements IFormattable
, the format specified mm:ss
would be passed to the ToString
call. Now this format mm:ss
is invalid for TimeSpan
, it requires colon to be escaped with back slash like: mm\:ss
. That is why you are getting exception.
See: Custom TimeSpan Format Strings
In .Net 3.5 or lower you can use:
TimeSpan elapsed = DateTime.Now - renderStartTime; //or DateTime.Now.Subtract(renderStartTime)
string formatted = string.Format("Init took {0}:{1}", elapsed.Minutes, elapsed.Seconds); //returns minutes and seconds components,
// If you are looking for Total Minutes and Total Seconds then use TotalMinutes/TotalSeconds
In .Net framework 4.0 or higher you can do:
string.Format("Init took {0:mm\\:ss}", elapsed);