Niko Gamulin
Niko Gamulin

Reputation: 66565

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!

Upvotes: 5

Views: 9176

Answers (5)

D'Arcy Rittich
D'Arcy Rittich

Reputation: 171391

double seconds = (currentTime - startTime).TotalSeconds;

Upvotes: 9

Barry Kelly
Barry Kelly

Reputation: 42152

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

Upvotes: 3

Steve Willcock
Steve Willcock

Reputation: 26849

Unless I'm missing something:

t.TotalSeconds;

Upvotes: 0

David M
David M

Reputation: 72850

Try this:

relativeTime.TotalSeconds

This returns whole and fractional, as a double.

Upvotes: 2

Mehrdad Afshari
Mehrdad Afshari

Reputation: 421978

timeSpan.TotalSeconds

Upvotes: 1

Related Questions