Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, September 16, 2015 11:51 AM
time = stopwatch.Elapsed;
time is TimeSpan and stopwatch is StopWatch
The problem is that on seconds i'm getting 00:00:04.454545
I didn't check the minutes and hours but i'm guessing it's the same.
How can i make that the variable time will show the hours minutes and seconds as complete numbers without any digits after the point ? For example: 01:04:05 or 12:05:22 or 11:22:12
And not 12.343:01.343434:33.1111
All replies (5)
Wednesday, September 16, 2015 2:31 PM âś…Answered
I wanted to post this in your other thread but it did not want to go through (maybe because you deleted a reply there). Anyway, the timespan has a number of members (or whatever they are called in OOP) like days, minutes etc.
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
System.Threading.Thread.Sleep(1250);
sw.Stop();
TimeSpan ts = sw.Elapsed;
string message = string.Format("Upload completed, 100, {0:D2}:{1:D2}:{2:D2}:{3:D2}:{4:D3}", ts.Days, ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds);
Wednesday, September 16, 2015 1:50 PM
I don't understand, the syntax is correct for time. 4.454545 is 4 seconds and 454545 milliseconds. You always use a decimal to separate seconds from milliseconds. This is universal. The syntax of timespan is
DD:HH:MM:SS.mmm
It sounds like you are trying to use a custom format string instead of the standard format. The standard format will not generate the syntax you gave with decimals in the middle. It might if you use the invariant culture as documented here. In general you can simply use .ToString() with no parameters and get the format you want. The only time you need to use a format string is if you want to customize the behavior.
Michael Taylor
http://blogs.msmvps.com/p3net
Wednesday, September 16, 2015 4:01 PM | 1 vote
The problem is that on seconds i'm getting 00:00:04.454545
I didn't check the minutes and hours but i'm guessing it's the same.
Nope, not even close.
TimeSpan returns and takes integer values only. Like DateTime it internally stores the time(difference) as Ticks (a long integer; 1 tick = 100 Nanoseconds; 10,000 Ticks = 1 ms). All the other properties and outputs will just display a representation of the Ticks value.
Mind you, your computers and OS clock will definetly not run with that kind of accuracy to actually use ticks:
http://blogs.msdn.com/b/ericlippert/archive/2010/04/08/precision-and-accuracy-of-datetime.aspx
Even measuring below 20 ms resolution is pretty unrealistic.
Wednesday, September 16, 2015 4:47 PM | 1 vote
4.454545 is 4 seconds and 454545 milliseconds.
Not quite ;) 4 seconds, 454 milliseconds and 545 microseconds
Wednesday, September 16, 2015 6:19 PM | 1 vote
TimeSpan is accurate to ms so microseconds aren't going to work out (the fact that it stores ticks as nanosecond intervals isn't really relevant). If you need that kind of accuracy then you're going to have to use Ticks and do the conversion yourself. But be aware that TimeSpan's limit of ms means that some methods may not work correctly for you (like addition).
People have run into trouble trying to use TimeSpan for this kind of precision. It is recommended that you use one of the third party libraries available to represent time that is more precise if you need it. Alternatively if you are timing how long things take then Stopwatch will work. It can be manipulated to provide microsecond information by doing the calculations yourself.