1.11M Members

how to subtract and to add time value in visual basic 2010

 
0
 

I'm Currently working with a payroll system with daily time record.. I'm having troubles with their time records, what i did is this, i created 6 textboxes on the form the 1st textbox is for the AM IN 2nd is for the Lunch OUT the 3rd Is for the total hours worked for the half day, and the same for the 4th to 6th textboxes. I actually get the difference of 2nd and 1st textboxes and show the result at 3rd textbox and same for the 4th-6th textboxes using this code that I also get in this site

Dim TimeA As Date
        Dim TimeB As Date
        Dim hh As Integer
        Dim mm As Integer
        Dim ss As Integer

        If Not Date.TryParse(TextBox1.Text, TimeA) Then
            ' Not a date
        End If
        If Not Date.TryParse(TextBox2.Text, TimeB) Then
            ' Not a date
        End If
        ' Subtract (= time <strong class="highlight">between</strong>)
        hh = TimeB.Subtract(TimeA).Hours
        mm = TimeB.Subtract(TimeA).Minutes
        ss = TimeB.Subtract(TimeA).Seconds
        TextBox3.Text = hh.ToString.PadLeft(2, CChar("0")) & ":" &   mm.ToString.PadLeft(2, CChar("0"))


        If Not Date.TryParse(TextBox4.Text, TimeA) Then
            ' Not a date
        End If
        If Not Date.TryParse(TextBox5.Text, TimeB) Then
            ' Not a date
        End If
        ' Subtract (= time <strong class="highlight">between</strong>)
        hh = TimeB.Subtract(TimeA).Hours
        mm = TimeB.Subtract(TimeA).Minutes
        ss = TimeB.Subtract(TimeA).Seconds
        TextBox6.Text = hh.ToString.PadLeft(2, CChar("0")) & ":" & mm.ToString.PadLeft(2, CChar("0"))

but i really didn't understand it so right now I don't know how to add the value of textbox3 and textbox6 so I could get the total hours worked of the employee.. pls teach me! i really want to learn vb and I would tell it right now I'm a bit slow so I need a lot of your patience, thanks a lot.

 
0
 

i forgot to tell that i have a 7th textbox that suppose to display the total hours of txtbx3 and txtbx6,

I also try this code

Dim d1 As DateTime
        Dim d2 As DateTime
        Dim ts As TimeSpan

        'Time format is 10:35:23. So in textbox you must type like this...
        d1 = DateTime.Parse("1/1/2012 " + Me.TextBox1.Text.Trim)
        d2 = DateTime.Parse("1/1/2012 " + Me.TextBox2.Text.Trim)
        ts = d2 - d1

        TextBox3.Text = (ts.ToString)

        'Time format is 10:35:23. So in textbox you must type like this...
        d1 = DateTime.Parse("1/1/2012 " + Me.TextBox4.Text.Trim)
        d2 = DateTime.Parse("1/1/2012 " + Me.TextBox5.Text.Trim)
        ts = d2 - d1

        TextBox6.Text = (ts.ToString)


        TextBox7.Text = (Val(TextBox3.Text) + Val(TextBox6.Text))

but the value of textbox3 and textbox6 is been coverted to string.
so I can't add those as time value. I don't know how to do it..pls help me

 
0
 

Here is an example

Dim amin As DateTime = CDate("08:00:00")
Dim amout As DateTime = CDate("12:05:00")
Dim pmin As DateTime = CDate("13:30:00")
Dim pmout As DateTime = CDate("16:43:00")

Dim amhours As TimeSpan = amout - amin
Dim pmhours As TimeSpan = pmout - pmin
Dim total As TimeSpan = amhours + pmhours

MsgBox(total.ToString)
 
0
 

Im very sory Sir but I did not get it, I've tried the code but the results is always the same sir.. sir pls teach me how will i do it. sory again for being slow,

 
1
 

Ok you can try this...

Dim d1 As DateTime
        Dim d2 As DateTime
        Dim ts As TimeSpan
Dim ts2 As TimeSpan
Dim tsFinal As TimeSpan

        'Time format is 10:35:23. So in textbox you must type like this...
        d1 = DateTime.Parse("1/1/2012 " + Me.TextBox1.Text.Trim)
        d2 = DateTime.Parse("1/1/2012 " + Me.TextBox2.Text.Trim)
        ts = d2 - d1

        TextBox3.Text = (ts.ToString)

        'Time format is 10:35:23. So in textbox you must type like this...
        d1 = DateTime.Parse("1/1/2012 " + Me.TextBox4.Text.Trim)
        d2 = DateTime.Parse("1/1/2012 " + Me.TextBox5.Text.Trim)
        ts2 = d2 - d1

        TextBox6.Text = (ts2.ToString)

tsFinal=ts+ts2
        TextBox7.Text =tsFinal.ToString
Question Answered as of 2 Years Ago by kingsonprisonic and Reverend Jim
 
0
 

thank you very much sir.. it really helps a lot.. thanks and have a great day sir

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: