Learn how to a build a cloud-first strategyRegister Now

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

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.
Thanks in advance

~andy
0
Andy Keeney
Asked:
Andy Keeney
  • 3
  • 3
1 Solution
 
Mike TomlinsonMiddle 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
        dtB = dtA.AddHours(2)
        dtB = dtB.AddMinutes(3)
        dtB = dtB.AddSeconds(41)

        ' 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
 
mjwillsCommented:
VB 6 or VB.NET?
0
 
Mike TomlinsonMiddle 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Andy KeeneyAuthor 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
        dtB = dtA.AddHours(2)
        dtB = dtB.AddMinutes(3)
        dtB = dtB.AddSeconds(41)
"

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
 
mjwillsCommented:
       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
 
Mike TomlinsonMiddle 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
 
mjwillsCommented:
Agreed - a split (40 Idle_Mind, 10 me) does seem in order.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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