how do i find time diffrence

Posted on 2004-10-25
Last Modified: 2010-04-24
im trying to find time diffrence between two times... in hours,mins,sec

10/24/2004 12:12:47 AM
10/24/2004 12:12:49 AM

in this case its 0 hours, 0 mins, and 2 seconds

the format for the date - time string will stay the same... in each case...

aka Pern

Question by:Johnny
    LVL 14

    Expert Comment

    Use the DataDiff function ... example:

    LVL 6

    Expert Comment

    Date.Subtract returns a TimeSpan object, from which you can get the total days, seconds, minutes, etc.

    Msgbox(Date.Subtract(Now, MyDate).TotalSeconds.ToString)

    LVL 4

    Expert Comment


    Dim a as Date = #10/24/2004 12:12:47 AM#
    Dim b as Date = #10/24/2004 12:12:49 AM#
    Dim diff As TimeSpan = b.Subtract(a)

    Then you can access to all the TimeSpan properties : for example diff.Seconds() to have the number of second elapsed between a and b.

    LVL 85

    Accepted Solution

    I agree with KGreg and Glom but if you are getting the dates as strings then use DateTime.Parse() first to convert them to dates and then proceed to use the .Subtract() function with the TimeSpan() class:

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim strDateA As String = "10/24/2004 12:12:47 AM"
            Dim strDateB As String = "10/24/2004 12:12:49 AM"

            Dim dateA As DateTime = DateTime.Parse(strDateA)
            Dim dateB As DateTime = DateTime.Parse(strDateB)

            Dim ts As TimeSpan = dateB.Subtract(dateA)

            MsgBox(Format(ts.Hours, "00") & ":" & Format(ts.Minutes, "00") & ":" & Format(ts.Seconds, "00"), MsgBoxStyle.Information, "Difference")
        End Sub


    Author Comment

    Perfect thx

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Shellfire Box VPN + Lifetime Subscription

    The Shellfire Box easily connects all of your devices, even those that don't offer the possibility to establish a safe vpn connection. Access blocked content and surf safely, no matter where in the world you are located.

    I'm currently working for a company where I have to upgrade over 50 VB6 programs to VB.NET 2008.  So far I'm about half way through, and I've learned quite a few tricks that drastically improve the performance of VB.NET apps. Because there are a…
    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    857 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now