i have here a code that add timeintextbox and timeouttextbox to calculate for the total hours work. my problem is that i want to subtract 1hour in the total hours work for the break time..please any idea on how should i do this?

 Dim TimeA As Date
    Dim TimeB As Date
    Dim hh As Integer
    Dim mm As Integer
    Dim ss As Integer
    If Not Date.TryParse(TimeInTextBox.Text, TimeA) Then
        ' Not a date
    End If
    If Not Date.TryParse(TimeOutTextBox.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
    TotalHWTextBox.Text = hh.ToString.PadLeft(2, CChar("0")) & ":" & mm.ToString.PadLeft(2, CChar("0"))

    Dim d1 As DateTime
    Dim d2 As DateTime
    Dim ts As TimeSpan
    Dim ts2 As TimeSpan
    'Time format is 10:35:23. So in textbox you must type like this...
    d1 = DateTime.Parse(Me.TimeInTextBox.Text.Trim)
    d2 = DateTime.Parse(Me.TimeOutTextBox.Text.Trim)
    ts = d2 - d1
           TotalHWTextBox.Text = (ts.ToString)

In ts you subtract 60 minutes if it is a timespan, before showing or assigning to the text box TotalHWtextbox.

or just use a if hh > 1 then hh = hh - 1 else 'whatever you want here kind of logic before your TotalHWTextBox.Text = hh.ToString.PadLeft(2, CChar("0")) & ":" & mm.ToString.PadLeft(2, CChar("0")) code.

i have here new code..i want again to minus 1hour to the total hours work for the breaktime..how can i do this? please help

Dim TimeOutHours = TimeSpan.FromHours(Val(DateTime.Parse(TimeOfDay.ToString(Me.TimeOutTextBox.Text.Trim)).Hour))
    Dim TimeInHours = TimeSpan.FromHours(Val(DateTime.Parse(TimeOfDay.ToString(Me.TimeInTextBox.Text.Trim)).Hour))
    Dim TimeOutMins As Integer = (Val(DateTime.Parse(TimeOfDay.ToString(TimeOutTextBox.Text)).Minute))
    Dim TimeInMins As Integer = (Val(DateTime.Parse(TimeOfDay.ToString(TimeInTextBox.Text)).Minute))
    Dim mingreat As TimeSpan = TimeOutHours.Subtract(TimeInHours).Subtract(TimeSpan.FromHours(1))
    Dim minless As TimeSpan = TimeOutHours.Subtract(TimeInHours)
    TotalHWTextBox.Text = minless.ToString

Never used timespans before but at a guess I'd say:

Dim BreakTime As TimeSpan  = TimeSpan.FromHours(1)
Dim minless As TimeSpan = TimeOutHours.Subtract(TimeInHours)
Dim LessLunch As TimeSpan = minless.Subtract(BreakTime)

Edited 4 Years Ago by G_Waddell

This article has been dead for over six months. Start a new discussion instead.