How show minutes and seconds with Stopwatch()

前端 未结 7 467
北荒
北荒 2021-02-01 12:20

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(\"F         


        
相关标签:
7条回答
  • 2021-02-01 13:02
    TimeTakenOutput.Text = "0" + myStopWatch.Elapsed.Minutes.ToString() 
     + ":" + myStopWatch.Elapsed.Seconds.ToString() + "mins";
    
    0 讨论(0)
  • 2021-02-01 13:04
    ts.Minutes
    

        

    0 讨论(0)
  • 2021-02-01 13:09

    //try it

           Stopwatch sw = new Stopwatch();
    
            sw.Start();
    
            Thread.Sleep(10382);
    
            sw.Stop();
    
    
            Console.Write(sw.Elapsed.Duration());
    
    0 讨论(0)
  • 2021-02-01 13:11

    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);
    
    0 讨论(0)
  • 2021-02-01 13:15

    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"))
    
    0 讨论(0)
  • 2021-02-01 13:17

    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.

    0 讨论(0)
提交回复
热议问题