C# + Format TimeSpan

c#

Solution

Try this:

Console.WriteLine("{0:D2}:{1:D2}", duration.Minutes, duration.Seconds);

Problem

I am trying to format a TimeSpan element in the format of "[minutes]:[seconds]". In this format, 2 minutes and 8 seconds would look like "02:08". I have tried a variety of options with String.Format and the ToString methods, but I get a FormatException. This is what I'm currently trying: ``` DateTime startTime = DateTime.Now; // Do Stuff TimeSpan duration = DateTime.Now.Subtract(startTime); Console.WriteLine("[paragraph of information] Total Duration: " + duration.ToString("mm:ss")); ``` What am I doing wrong? How do I format a TimeSpan element using my desired format?

Original source

Related problems