[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

how do i find time diffrence

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
Johnny
Asked:
Johnny
1 Solution
 
dfu23Commented:
Use the DataDiff function ... example:

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

0
 
KGregCommented:
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
 
GlomCommented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
JohnnyAuthor Commented:
Perfect thx
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now