66

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?

Oleks
  • 31,955
  • 11
  • 77
  • 132
ale
  • 3,301
  • 10
  • 40
  • 48
  • Console.WriteLine("File Generated: " + _writer.getBinaryFileName(filePath, Convert.ToInt32(logSelected)) + " in: " + " {0} minute(s)"+ " {1} second(s)", ts.Minutes, ts.Seconds, ts.Milliseconds/10 + "\n"); – ale Apr 18 '11 at 22:12
  • 3
    Please consider accepting the answer that worked best for you, ale. Just to keep the unanswered questions section "clean". Thank you! :) – Marcus Mangelsdorf Jan 21 '16 at 14:47
  • what is stopwatch? – barlop Mar 02 '19 at 16:14
  • @barlop Stopwatch "Provides a set of methods and properties that you can use to accurately measure elapsed time." See here for more information: https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.stopwatch?view=netframework-4.8 – supermeerkat Oct 10 '19 at 14:53

7 Answers7

90

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"))
Andrei
  • 4,122
  • 3
  • 22
  • 24
  • 6
    The problem, though, is if the total timespan is 70 minutes, it will show `10:00.00`. – Jim Mischel Apr 18 '11 at 22:13
  • 1
    Using string interpolation and format specifier: `$"Total processing time: {Math.Floor(ts.TotalMinutes)}:{ts:ss\\.ff}"` – defines Oct 16 '17 at 20:07
26

The TimeSpan.ToString() method in .NET 4.0 has an overload that lets you specify the format.

To display minutes and seconds:

TimeSpan elapsed = GetElapsedTime(); // however you get the amount of time elapsed
string tsOut = elapsed.ToString(@"m\:ss");

To include the milliseconds, you would write:

string tsOut = elapsed.ToString(@"m\:ss\.ff");

Note, however, that this won't do what you expect if the total timespan is more than 60 minutes. The "minutes" value displayed will be elapsed.Minutes, which is basically the same as ((int)elapsed.TotalMinutes) % 60). So if the total time was 70 minutes, the above will show 10:00.

If you want to show the total minutes and seconds reliably, you have to do the math yourself.

int minutes = (int)elapsed.TotalMinutes;
double fsec = 60 * (elapsed.TotalMinutes - minutes);
int sec = (int)fsec;
int ms = 1000 * (fsec - sec);
string tsOut = String.Format("{0}:{1:D2}.{2}", minutes, sec, ms);
Bart Verkoeijen
  • 16,545
  • 7
  • 52
  • 56
Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
  • Nice solution, but "int ms = 1000 * (fsec - sec);" is wrong. – Mecanik Feb 09 '18 at 12:26
  • @NorbertBoros Explain your reasoning. Better yet, write a little test program to verify your assertion, and show me the results. – Jim Mischel Feb 09 '18 at 14:18
  • 1
    I believe it is pretty clear, error CS0266: Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists (are you missing a cast?). – Mecanik Feb 09 '18 at 14:44
22

I've coded this way:

using System.Diagnostics;
...

Stopwatch watch = new Stopwatch();
watch.Start();

// here the complex program.
...

watch.Stop();

TimeSpan timeSpan = watch.Elapsed;

Console.WriteLine("Time: {0}h {1}m {2}s {3}ms", timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds, timeSpan.Milliseconds);
Eduardo Pelais
  • 1,627
  • 15
  • 21
9

//try it

       Stopwatch sw = new Stopwatch();

        sw.Start();

        Thread.Sleep(10382);

        sw.Stop();


        Console.Write(sw.Elapsed.Duration());
Felipe Augusto
  • 1,341
  • 1
  • 16
  • 18
8

Review the documentation for TimeSpan, the struct returned by stopwatch.Elapsed. You want either the Minutes or TotalMinutes property.

If you're measuring a 62 minute span, ts.Minutes will return 2, and ts.TotalMinutes will return 62.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
  • 8
    Have to be careful, though. If the total time span is 62 minutes and 30 seconds, `ts.TotalMinutes` will return `62.5`. – Jim Mischel Apr 18 '11 at 22:03
0
TimeTakenOutput.Text = "0" + myStopWatch.Elapsed.Minutes.ToString() 
 + ":" + myStopWatch.Elapsed.Seconds.ToString() + "mins";
Mayank Pathak
  • 3,621
  • 5
  • 39
  • 67
PLS HELP
  • 5
  • 4
-3
ts.Minutes

    

Mark Cidade
  • 98,437
  • 31
  • 224
  • 236