Calculate duration using Date.Time.Now in C #

I need to calculate the duration of my system. My system is in C #. I have installed:

DateTime startRunningProg = Date.Time.Now("o"); 

after several processes.

I have installed:

 DateTime endRunningProg = Date.Time.Now("o"); 

How to calculate the duration of my system, working in milliseconds or seconds.

+4
source share
3 answers

To accurately measure elapsed time, use the StopWatch class:

 Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); Thread.Sleep(10000); stopWatch.Stop(); // Get the elapsed time as a TimeSpan value. TimeSpan ts = stopWatch.Elapsed; // Format and display the TimeSpan value. string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); Console.WriteLine("RunTime " + elapsedTime); 
+14
source
  (endRunningProg - startRunningProg).TotalMilliseconds ; 

But @avs is right - use the stopwatch class. See This Question. Stopwatch versus using System.DateTime.Now for synchronization events.

+4
source

As already mentioned, if you are trying to do exact timings, then you should use the StopWatch class.

If you really want to do the math Date and find out the difference between the two dates, I suggest you check out Noda Time

0
source

All Articles