views:

228

answers:

5

Hi,

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!

+5  A: 

double seconds = (currentTime - startTime).TotalSeconds;

RedFilter
+2  A: 

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
Barry Kelly
Should that be "/ 1000.0"?
Martin Harris
Yep. Fixed. Now to fill minimal comment char count.
Barry Kelly
+1  A: 
timeSpan.TotalSeconds
Mehrdad Afshari
+1  A: 

Try this:

relativeTime.TotalSeconds

This returns whole and fractional, as a double.

David M
A: 

Unless I'm missing something:

t.TotalSeconds;
Steve Willcock