We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,657 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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

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.

3
Contributors
5
Replies
10 Hours
Discussion Span
1 Year Ago
Last Updated
8
Views
Question
Answered
kishpopboy
Newbie Poster
15 posts since Feb 2012
Reputation Points: 9
Solved Threads: 0
Skill Endorsements: 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

kishpopboy
Newbie Poster
15 posts since Feb 2012
Reputation Points: 9
Solved Threads: 0
Skill Endorsements: 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)
Reverend Jim
Carpe per diem
Moderator
3,587 posts since Aug 2010
Reputation Points: 561
Solved Threads: 445
Skill Endorsements: 32

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,

kishpopboy
Newbie Poster
15 posts since Feb 2012
Reputation Points: 9
Solved Threads: 0
Skill Endorsements: 0

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
kingsonprisonic
Posting Whiz in Training
268 posts since Nov 2009
Reputation Points: 61
Solved Threads: 54
Skill Endorsements: 0
Question Answered as of 1 Year Ago by kingsonprisonic and Reverend Jim

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

kishpopboy
Newbie Poster
15 posts since Feb 2012
Reputation Points: 9
Solved Threads: 0
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0720 seconds using 2.72MB