Solved

how do i find time diffrence

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

example
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...

thx
Johnny
aka Pern

0
Question by:Johnny
    5 Comments
     
    LVL 14

    Expert Comment

    by:dfu23
    Use the DataDiff function ... example:

    http://www.experts-exchange.com/Programming/Q_21069414.html

    0
     
    LVL 6

    Expert Comment

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

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

    KGREG
    0
     
    LVL 4

    Expert Comment

    by:Glom
    Hi,

    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.

    Glom
    0
     
    LVL 85

    Accepted Solution

    by:
    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

    ~IM
    0
     

    Author Comment

    by:Johnny
    Perfect thx
    0

    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 vb.net 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 (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". 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 (http://www.enterprise.efax.com), 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