Difference in seconds from two datetime objects

  • Thread starter Thread starter Ron
  • Start date Start date
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
 
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.
 
Back
Top