简体   繁体   中英

How to get the hour as a total

i'm having a problem. How to get the total hour? The output that's been displaying is not the sum that I've been expecting. Here's the SS so that you guys can get the picture.

在此处输入图片说明

I just need the total hour part. Thank you.

Here's the code for getting the total worked hour.

    Dim d1 As DateTime
    Dim d2 As DateTime
    Dim ts1 As TimeSpan
    Dim ts2 As TimeSpan
    Dim ts3 As TimeSpan
    Dim ts4 As TimeSpan
    Dim ts5 As TimeSpan
    Dim ts6 As TimeSpan
    Dim tsFinal As TimeSpan

    d1 = DateTime.Parse(txtTimeIn1.Text)
    d2 = DateTime.Parse(txtTimeOut1.Text)
    ts1 = d2 - d1
    txtTotalHr1.Text = (ts1.ToString)

    d1 = DateTime.Parse(txtTimeIn2.Text)
    d2 = DateTime.Parse(txtTimeOut2.Text)
    ts2 = d2 - d1
    txtTotalHr2.Text = (ts2.ToString)

    d1 = DateTime.Parse(txtTimeIn3.Text)
    d2 = DateTime.Parse(txtTimeOut3.Text)
    ts3 = d2 - d1
    txtTotalHr3.Text = (ts3.ToString)

    d1 = DateTime.Parse(txtTimeIn4.Text)
    d2 = DateTime.Parse(txtTimeOut4.Text)
    ts4 = d2 - d1
    txtTotalHr4.Text = (ts4.ToString)

    d1 = DateTime.Parse(txtTimeIn5.Text)
    d2 = DateTime.Parse(txtTimeOut5.Text)
    ts5 = d2 - d1
    txtTotalHr5.Text = (ts5.ToString)

    d1 = DateTime.Parse(txtTimeIn6.Text)
    d2 = DateTime.Parse(txtTimeOut6.Text)
    ts6 = d2 - d1
    txtTotalHr6.Text = (ts6.ToString)

    tsFinal = ts1 + ts2 + ts3 + ts4 + ts5 + ts6
    txtTotalWorkHour.Text = tsFinal.ToString

Try this

Function GetTotalTime() As TimeSpan
        Dim count As TimeSpan = TimeSpan.Zero
        For Each x As DataGridViewRow In Table.Rows
            count += TimeSpan.Parse(x.Cells(columnIndex).Value)

        Next
        Return count
End Function


'elsewhere
Dim hrs As Integer = GetTotalTime().Hours

You have a formatting issue. The default TimeSpan ToString format is d.hh:mm:ss, where d is days. So 2:11:03:00 means 2 days, 11 hours, and 3 minutes. I assume you wanted 59:03:00, meaning 59 hours (24 * 2 + 11), and 3 minutes. To accomplish that, use the following formatting:

txtTotalWorkHour.Text = String.Format("{0:n0}:{1:d2}:{2:d2}", tsFinal.TotalHours, tsFinal.Minutes, tsFinal.Seconds)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM