Learn how to a build a cloud-first strategyRegister Now

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 176

difference in time

i assume this question will show how shallow my knowledge of vb.net is but here goes...

i need to compare 2 different times.  the times occur same day, and i can write code that will split the hours and minutes and subtract them and then divide the minute end result by 60...blah blah...blah
but what i want to know is there a shorter way to do this? and if so please explain.

~andy
0
Andy Keeney
• 3
• 3
1 Solution

Middle School Assistant TeacherCommented:
In VB.Net you can use the DateTime() and TimeSpan() classes:

Private Sub foo()
Dim dtA As DateTime
Dim dtB As DateTime
Dim ts As TimeSpan

' set dtA to the currentTime
dtA = DateTime.Now()

' set dtB to dtA plus 2 hrs, 3 mins and 41 seconds

' set ts equal to the difference between dtA and dtB
ts = dtB.Subtract(dtA)

' output the various differences
Debug.WriteLine("ts = " & ts.ToString)
Debug.WriteLine("Hours: " & ts.Hours)
Debug.WriteLine("TotalHours: " & ts.TotalHours)
Debug.WriteLine("Minutes: " & ts.Minutes)
Debug.WriteLine("TotalMinutes: " & ts.TotalMinutes)
Debug.WriteLine("Seconds: " & ts.Seconds)
Debug.WriteLine("TotalSeconds: " & ts.TotalSeconds)
End Sub
0

Commented:
VB 6 or VB.NET?
0

Middle School Assistant TeacherCommented:
The authors very first line states that it is for VB.Net:

>> i assume this question will show how shallow my knowledge of vb.net is but here goes...
0

Author Commented:
in your example, you know how much time is in between the two times:
"
' set dtB to dtA plus 2 hrs, 3 mins and 41 seconds
"

that i s what i want to find out.  how much time is between dta and datb
where dta = 9:30 AM and dtb=6:21 PM
0

Commented:
Dim dtA As DateTime
Dim dtB As DateTime
Dim ts As TimeSpan

' set dtA to the currentTime
dtA = #9:30:00 AM#
dtB = #6:21:00 PM#

' set ts equal to the difference between dtA and dtB
ts = dtB.Subtract(dtA)

' output the various differences
Debug.WriteLine("ts = " & ts.ToString)
Debug.WriteLine("Hours: " & ts.Hours)
Debug.WriteLine("TotalHours: " & ts.TotalHours)
Debug.WriteLine("Minutes: " & ts.Minutes)
Debug.WriteLine("TotalMinutes: " & ts.TotalMinutes)
Debug.WriteLine("Seconds: " & ts.Seconds)
Debug.WriteLine("TotalSeconds: " & ts.TotalSeconds)
0

Middle School Assistant TeacherCommented:
No split?

mjwills literally copied my code and simply changed the start/stop times...the core of the question was how to "split the hours and minutes" to show the difference in time which I demonstrated via the TimeSpan() class.

In your program, where are dtA and dtB coming from?  A database?   ...or are they being entered by the user?  I seriously doubt that you are going to need to hard code the time into your application like that.

Do you need to be able to build the time variable from user input?
0

Commented:
Agreed - a split (40 Idle_Mind, 10 me) does seem in order.
0

Featured Post

• 3
• 3
Tackle projects and never again get stuck behind a technical roadblock.