String.Format exception following upgrade from .Net 2.0 to .Net 4.5.2
.net, asp.net, c#
Solution
The problem is the second colon. You can do the following instead
string.Format("Init took {0:mm}:{0:ss}", DateTime.Now.Subtract(renderStartTime));
EDIT
Based on how `TimeSpan` worked in .Net 3.5 and earlier the original code in .Net 2.0 would actually print out the time in a hh:mm:ss format. Basically the "mm:ss" part of the format was ignored because `TimeSpan` did not implement `IFormattable`. So the following in .Net 4.5.2 is really closer to replacing what the original code was doing in .Net 2.0.
string.Format("Init took {0}", DateTime.Now.Subtract(renderStartTime));
But if you want to only show minutes and seconds you can use formatting of either `{0:mm}:{0:ss}` or `{0:mm\\:ss}` in .Net 4.0 and higher. Or in .Net 3.5 or earlier you would have to do it like this.
TimeSpan diff = DateTime.Now.Subtract(renderStartTime);
string.Format("Init took {0:00}:{1:00}", diff.Minutes, diff.Seconds);
Problem
I have recently upgraded a ASP.Net forms application to .Net 4.5.2, after fixing some relatively trivial namespace issues I was able to build the solution successfully. However at runtime I have been receiving the following error: An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code Which when debugging is thrown by the following line: ``` string.Format("Init took {0:mm:ss}", (object) DateTime.Now.Subtract(renderStartTime)) ``` Where renderStartTime = DateTime.Now I am somewhat puzzled why I am seeing this error since upgrading. Any thoughts?