Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate double value from TimeSpan

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!

like image 470
Niko Gamulin Avatar asked May 20 '09 12:05

Niko Gamulin


4 Answers

double seconds = (currentTime - startTime).TotalSeconds;

like image 117
D'Arcy Rittich Avatar answered Nov 20 '22 04:11

D'Arcy Rittich


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
like image 41
Barry Kelly Avatar answered Nov 20 '22 05:11

Barry Kelly


Try this:

relativeTime.TotalSeconds

This returns whole and fractional, as a double.

like image 41
David M Avatar answered Nov 20 '22 05:11

David M


timeSpan.TotalSeconds
like image 1
mmx Avatar answered Nov 20 '22 05:11

mmx