Reputation: 704
I have this code:
private void TimePlayedTimer_Start()
{
timePlayedStr = "00:00:00";
timePlayed = new DispatcherTimer();
timePlayed.Tick += timePlayedTimer_Tick;
timePlayed.Interval = new TimeSpan(0, 0, 0, 1);
timePlayed.Start();
}
void timePlayedTimer_Tick(object sender, object e)
{
TimeSpan ts = TimeSpan.Parse(timePlayedStr);
ts = ts.Add(TimeSpan.FromSeconds(1));
timePlayedStr = ts.ToString();
}
When I debug this line by line, TimeSpan ts
would equal "00:00:00"
but after line ts = ts.Add(TimeSpan.FromSeconds(1));
it would some how have properties TotalDays = 2.313232439423
, TotalHours = 0.000555555
, TotalMilliseconds = 2000
rather than adding a 1
to the TotalSeconds
properties I get these property values returned.
Does anyone know what I am doing wrong?
PS: I am just trying to add a second to the TimeSpan
after every tick
Upvotes: 0
Views: 2636
Reputation: 700432
The value for TotalDays
is actually 2.31481481481481E-05
, i.e. 0.0000231481481481481
.
The value that you get is exactly what's expected at the second tick, you didn't manage to debug the first tick, and you are just interpreting the values wrong.
The TotalDays
, TotalHours
and TotalMilliseconds
properties show the total value in the TimeSpan
translated to that specific measurement, they don't form a value together.
2 seconds is the same as 2000 milliseconds, and the same as 0.000555555 hours.
If you want to look at the components in the value, you should look at the Days
, Hours
, Minutes
, Seconds
and Milliseconds
properties. There you will find that the Seconds
property is 2
and all the others are zero.
Upvotes: 6
Reputation: 18155
I think you're misreading the TotalDays value. When I run similar code I get my TotalDays
value of 1.15740740740741E-05
. That likely makes sense, one second is probably roughly that fraction of a day.
The Total*
properties represent the overall value of the TimeSpan
, not the discrete value of each portion of the TimeSpan
.
Days
, Hours
, and Minutes
will all be 0, but the Total*
properties will represent the entirety of the value, even if those parts are fractional.
Upvotes: 3