• Status: Solved
• Priority: Medium
• Security: Public
• Views: 523

# vb.net time calculation

I have sixcomboboxes:

1st show hrs
2nd shows minutes
3rd show AM or PM

1st show hrs
2nd shows minutes
3rd show AM or PM

I want to add then up to get a total.

how do you do that, being that I am showing it to the user as real time and not military time.

0
hogan9111
1 Solution

Middle School Assistant TeacherCommented:
Please define what you mean by "I want to add then up to get a total."

~IM
0

Author Commented:
get the total hrs

meaning

and 5:00 PM

should be 4 hrs

1:15 PM
5:00 PM

should be
3.75
0

Commented:
OH. you mean you want the time delta between start and end time.

'First, convert the date/time to minutes since midnight:
totalMinStart=startMinutes
if startAMPM="PM" then

totalMinEnd=EndMinutes
if EndAMPM="PM" then
hoursEnd=hoursEnd+12
totalMinEnd=hoursEnd*60

'compute delta
deltaMin=totalMinEnd-totalMinStart

'Split into hours:minutes
deltaHours=CInt(deltaMin/60)    'Nearest hour;  Fraction of an hour is discarded
deltaMin=deltaMin % 60   'Get the remainder

return deltaHours+":"+deltaMin
0

Commented:
Just use DateDiff():
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vafctdatediff.asp

specifying whatever time granularity you want - seconds, minutes, hours, days, etc...
0

Middle School Assistant TeacherCommented:
Try this out:

Imports System.IO

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
Friend WithEvents ComboBox2 As System.Windows.Forms.ComboBox
Friend WithEvents ComboBox3 As System.Windows.Forms.ComboBox
Friend WithEvents ComboBox4 As System.Windows.Forms.ComboBox
Friend WithEvents ComboBox5 As System.Windows.Forms.ComboBox
Friend WithEvents ComboBox6 As System.Windows.Forms.ComboBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
Me.ComboBox1 = New System.Windows.Forms.ComboBox
Me.ComboBox2 = New System.Windows.Forms.ComboBox
Me.ComboBox3 = New System.Windows.Forms.ComboBox
Me.ComboBox4 = New System.Windows.Forms.ComboBox
Me.ComboBox5 = New System.Windows.Forms.ComboBox
Me.ComboBox6 = New System.Windows.Forms.ComboBox
Me.Button1 = New System.Windows.Forms.Button
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'ComboBox1
'
Me.ComboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
Me.ComboBox1.Location = New System.Drawing.Point(16, 8)
Me.ComboBox1.Name = "ComboBox1"
Me.ComboBox1.Size = New System.Drawing.Size(48, 21)
Me.ComboBox1.TabIndex = 0
'
'ComboBox2
'
Me.ComboBox2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
Me.ComboBox2.Location = New System.Drawing.Point(72, 8)
Me.ComboBox2.Name = "ComboBox2"
Me.ComboBox2.Size = New System.Drawing.Size(48, 21)
Me.ComboBox2.TabIndex = 1
'
'ComboBox3
'
Me.ComboBox3.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
Me.ComboBox3.Location = New System.Drawing.Point(128, 8)
Me.ComboBox3.Name = "ComboBox3"
Me.ComboBox3.Size = New System.Drawing.Size(48, 21)
Me.ComboBox3.TabIndex = 2
'
'ComboBox4
'
Me.ComboBox4.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
Me.ComboBox4.Location = New System.Drawing.Point(16, 48)
Me.ComboBox4.Name = "ComboBox4"
Me.ComboBox4.Size = New System.Drawing.Size(48, 21)
Me.ComboBox4.TabIndex = 3
'
'ComboBox5
'
Me.ComboBox5.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
Me.ComboBox5.Location = New System.Drawing.Point(72, 48)
Me.ComboBox5.Name = "ComboBox5"
Me.ComboBox5.Size = New System.Drawing.Size(48, 21)
Me.ComboBox5.TabIndex = 4
'
'ComboBox6
'
Me.ComboBox6.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
Me.ComboBox6.Location = New System.Drawing.Point(128, 48)
Me.ComboBox6.Name = "ComboBox6"
Me.ComboBox6.Size = New System.Drawing.Size(48, 21)
Me.ComboBox6.TabIndex = 5
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(8, 88)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(72, 24)
Me.Button1.TabIndex = 6
Me.Button1.Text = "Button1"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(88, 88)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(152, 20)
Me.TextBox1.TabIndex = 7
Me.TextBox1.Text = "TextBox1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(368, 286)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i As Integer

For i = 1 To 12
Next
For i = 0 To 59
Next

ComboBox1.SelectedIndex = 0
ComboBox2.SelectedIndex = 0
ComboBox3.SelectedIndex = 0
ComboBox4.SelectedIndex = 0
ComboBox5.SelectedIndex = 0
ComboBox6.SelectedIndex = 0
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim t1 As String = ComboBox1.Text & ":" & ComboBox2.Text & " " & ComboBox3.Text
Dim t2 As String = ComboBox4.Text & ":" & ComboBox5.Text & " " & ComboBox6.Text

Dim d1 As DateTime = DateTime.Parse(Now.ToShortDateString & " " & t1)
Dim d2 As DateTime = DateTime.Parse(Now.ToShortDateString & " " & t2)

If d2 < d1 Then
' assume the times span midnight
' and add one day to d2
End If

Dim ts As TimeSpan = d2.Subtract(d1)

TextBox1.Text = ts.TotalHours
End Sub

End Class
0

Author Commented:
thanks everyone the answers were great.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.