Solved

# vb.net time calculation

Posted on 2004-10-25
458 Views
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
Question by:hogan9111

LVL 85

Expert Comment

Please define what you mean by "I want to add then up to get a total."

~IM
0

Author Comment

get the total hrs

meaning

if I had 1:00 PM
and 5:00 PM

should be 4 hrs

or if I had

1:15 PM
5:00 PM

should be
3.75
0

LVL 21

Expert Comment

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

LVL 23

Expert Comment

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

LVL 85

Accepted Solution

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 Comment

thanks everyone the answers were great.
0

## Write Comment

Please enter a first name

Please enter a last name

## Featured Post

### Suggested Solutions

Title # Comments Views Activity
why to use string 10 47
Problem to adjust sheet 1 68
matchUp  challenge 9 31
array6 challenfge 6 28
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

#### 877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

#### Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!