Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How show minutes and seconds with Stopwatch()

Tags:

c#

stopwatch

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?

like image 631
ale Avatar asked Apr 18 '11 21:04

ale


People also ask

What is elapsed time in Stopwatch?

By default, the elapsed time value of a Stopwatch instance equals the total of all measured time intervals. Each call to Start begins counting at the cumulative elapsed time; each call to Stop ends the current interval measurement and freezes the cumulative elapsed time value.

How does Stopwatch work C#?

Stopwatch is a class in C# to measure the elapsed time. Use it to calculate the time a function took to execute. It is found under System. Diagnostics.

Can you get 0 milliseconds on a Stopwatch?

Since each operation is averaging 500/1000000 = 0.00005 ms, the ticks of the stopwatch will accumulate some small value, but the ElapsedMilliseconds (or Milliseconds of the TimeSpan) will still be 0.


1 Answers

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")) 
like image 183
Andrei Avatar answered Oct 05 '22 13:10

Andrei