C# Timespan Milliseconds vs TotalMilliseconds

Simple:

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

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


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

Tags:

C#

.Net

Timespan