How to generate double value from TimeSpan
Asked Answered
D

5

5

I have to calculate the relative time which is

TimeSpan relativeTime = currentTime.Subtract(startTime);

Next I would like to convert relativeTime to double value which should be consisted of seconds and milliseconds (seconds.milliseconds).

Does anyone know what is the best way to generate such double value from time difference?

Thanks!

Dynamoelectric answered 20/5, 2009 at 12:57 Comment(0)
I
9

double seconds = (currentTime - startTime).TotalSeconds;

Inchmeal answered 20/5, 2009 at 13:2 Comment(0)
T
3

Eh, TimeSpan.TotalSeconds. Or if you explicitly want to attempt a granularity of milliseconds (not totally possible with double), then:

((long) relativeTime.TotalMilliseconds) / 1000.0
Trifle answered 20/5, 2009 at 13:2 Comment(1)
Yep. Fixed. Now to fill minimal comment char count.Trifle
S
2

Try this:

relativeTime.TotalSeconds

This returns whole and fractional, as a double.

Saintpierre answered 20/5, 2009 at 13:2 Comment(0)
S
1
timeSpan.TotalSeconds
Stein answered 20/5, 2009 at 13:2 Comment(0)
B
0

Unless I'm missing something:

t.TotalSeconds;
Bridal answered 20/5, 2009 at 13:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.