Difference in seconds from two datetime objects

R

Ron

How can I find the difference in seconds of two DateTime objects. I have
looked at the Subtract method but this gives me back another DateTime
object.


Thanks,

Ron
 
M

mikeb

Ron said:
How can I find the difference in seconds of two DateTime objects. I have
looked at the Subtract method but this gives me back another DateTime
object.

Actually, DateTime.Subtract( TimeSpan) returns a DateTime object, but
DateTime.Subtract( DateTime) gives you a TimeSpan object. The
TotalSeconds property of this will give you what you want:

(datetime2.Subtract( datetime1)).TotalSeconds

or

(datetime2 - datetime1).TotalSeconds

Be careful not to mistakenly use the "Seconds" property of the TimeSpan
object, as this only gives you the seconds *component* of the TimeSpan,
which will always be in the range 0-59.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top