find the difference between two datetimes in seconds

33,388

Solution 1

Subtract the DateTime values from each other - the returned type will be a TimeSpan.

Get the TotalSeconds value from it.

(date1 - date2).TotalSeconds

Solution 2

There actually is also a function made for this DateDiff(DateInterval.Second, d1, d2)

Share:
33,388
Pezzzz
Author by

Pezzzz

Updated on July 05, 2022

Comments

  • Pezzzz
    Pezzzz almost 2 years

    I have had a quick look around and I can't find an answer to this in vb.net or something that I can convert into vb.net.

    I have two DateTimes in vb.net's 'Date' class. I would like to find the difference between these in seconds. I can do a-b, but the answer will still be a 'date'. I can use .seconds .minutes etc. and multiply but I will hit problems when I come to months.

    Is there a simple way to do this, or do I need to write some elaborate-ish code?

    Many Thanks

  • Marty_in_a_Box
    Marty_in_a_Box over 8 years
    Works like a charm. Thank you :)
  • Vijay Kumbhoje
    Vijay Kumbhoje over 7 years
    Why I am getting result as 63616023225.5405