Solved

vb.net time calculation

Posted on 2004-10-25
458 Views
Last Modified: 2006-11-17
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
    6 Comments
     
    LVL 85

    Expert Comment

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

    ~IM
    0
     

    Author Comment

    by:hogan9111
    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

    by:MogalManic
    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
        hoursStart=hoursStart+12
    totalMinStart=hoursStart*60

    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

    by:cookre
    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

    by:
    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.Controls.Add(Me.TextBox1)
            Me.Controls.Add(Me.Button1)
            Me.Controls.Add(Me.ComboBox6)
            Me.Controls.Add(Me.ComboBox5)
            Me.Controls.Add(Me.ComboBox4)
            Me.Controls.Add(Me.ComboBox3)
            Me.Controls.Add(Me.ComboBox2)
            Me.Controls.Add(Me.ComboBox1)
            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
                ComboBox1.Items.Add(i)
                ComboBox4.Items.Add(i)
            Next
            For i = 0 To 59
                ComboBox2.Items.Add(Format(i, "00"))
                ComboBox5.Items.Add(Format(i, "00"))
            Next
            ComboBox3.Items.Add("AM")
            ComboBox3.Items.Add("PM")
            ComboBox6.Items.Add("AM")
            ComboBox6.Items.Add("PM")

            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
                d2 = d2.AddDays(1)
            End If

            Dim ts As TimeSpan = d2.Subtract(d1)

            TextBox1.Text = ts.TotalHours
        End Sub

    End Class
    0
     

    Author Comment

    by:hogan9111
    thanks everyone the answers were great.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

     Java Android Coding Bundle

    Whether you're an Apple user or Android addict, learning to code for the Android platform is an extremely valuable, in-demand skill. It all starts with Java, the language behind the apps and games that make Android the top platform it is today.

    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.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now