How show minutes and seconds with Stopwatch()

ale picture ale · Apr 18, 2011 · Viewed 82.4k times · Source

I need to show also the minutes, actually I use this code for show the seconds, but also need the minutes

TimeSpan ts = stopwatch.Elapsed;
Console.WriteLine("File Generated: " + _writer.getBinaryFileName(filePath, Convert.ToInt32(logSelected)) + " in "  + "{0}.{1:D2}" + "seconds", 
    ts.Seconds, 
    ts.Milliseconds/10 + "\n"
);

how can I do?

Answer

Andrei picture Andrei · Apr 19, 2011

You should use:

ts.ToString("mm\\:ss\\.ff")

this will give you minutes, seconds and the hundredths of a second in a time interval.

also take a look at http://msdn.microsoft.com/en-us/library/ee372287.aspx

EDITED: well if you want minutes be your biggest unit you can do the following:

string.Format("{0}:{1}", Math.Floor(ts.TotalMinutes), ts.ToString("ss\\.ff"))