C# Timespan Milliseconds vs TotalMilliseconds

C#.NetTimespan

C# Problem Overview


In the example below, why does the Milliseconds property return 0 but the TotalMilliseconds property return 5000?

// 5 seconds
TimeSpan intervalTimespan = new TimeSpan(0, 0, 5);

// returns 0
intervalTimespan.Milliseconds;

// returns 5000.0
intervalTimespan.TotalMilliseconds

C# Solutions


Solution 1 - C#

Simple:

  • Milliseconds are the remaining milliseconds, that don't form a whole second.
  • TotalMilliseconds is the complete duration of the timespan expressed as milliseconds.

Solution 2 - C#

Because Milliseconds returns the Milliseconds portion, and TotalMilliseconds returns the total milliseconds represented by the Timespan

Example: 0:00:05.047

Milliseconds: 47

Total Milliseconds: 5047

Solution 3 - C#

This hapens because intervalTimespan.Milliseconds returns the millisecond component of the timespan. In your timespan constructor, you only have hour, minute, and second components, which is why the result is 0.

intervalTimespan.TotalMilliseconds gets you the total milliseconds of the timespan.

Example:

// 5 milliseconds
TimeSpan intervalTimespan = new TimeSpan(0, 0,0,0,5);

// returns 5
intervalTimespan.Milliseconds;

// returns 5
intervalTimespan.TotalMilliseconds

Solution 4 - C#

TimeSpan has other overloads:

TimeSpan(hour, minute, seconds)
TimeSpan(days, hour, minute, seconds)
TimeSpan(days, hour, minute, seconds, milliseconds)

The Milliseconds property returns the actual milliseconds value.

The TotalMilliseconds property returns the overall milliseconds including days, hours, minutes, and seconds.

Solution 5 - C#

Miliseconds returns just the milliseconds part of your TimeSpan, while TotalMilliseconds calculates how many milliseconds are in time represented by TimeSpan.

In your case, first returns 0 because you have exactly 5 seconds, second returns 5000 because 5s == 5000ms

Solution 6 - C#

One important thing other things don't mention, is that (according to the docs):

> The Milliseconds property represents whole milliseconds, whereas the TotalMilliseconds property represents whole and fractional milliseconds.

That is also deductible from the remarks of TotalMilliseconds:

> This property converts the value of this instance from ticks to milliseconds.

This has a huge implication, IMO, because if you want the most precise representation in seconds or milliseconds, you must use TotalSeconds or TotalMilliseconds properties, both of them are of type double.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAJMView Question on Stackoverflow
Solution 1 - C#Daniel HilgarthView Answer on Stackoverflow
Solution 2 - C#kͩeͣmͮpͥ ͩView Answer on Stackoverflow
Solution 3 - C#Marginean VladView Answer on Stackoverflow
Solution 4 - C#hallieView Answer on Stackoverflow
Solution 5 - C#JarekView Answer on Stackoverflow
Solution 6 - C#heltonbikerView Answer on Stackoverflow