# Calculation of time(working hours)

• ### Question

• Hi!

What I want to achieve is to be able to calculate total time for each name on a array!

The format of the data is like showed in photo

I have the code to calculate total time for a specific name and is working ok!

``` Dim totalHours As Integer
Dim totalMinutes As Integer
For Each S As String In ListBox2.Items
If S.Contains("Gabriel") And Not S.Contains("*") Then
totalHours += S.Split(" "c)(3).Split(":")(0)
totalMinutes += S.Split(" "c)(3).Split(":")(1)
End If
Next
Dim remainder = totalMinutes Mod 60
totalHours += totalMinutes \ 60
Dim totalTime1 = totalHours.ToString("d2") & ":" & remainder.ToString("d2")
ListBox3.Items.Add("Gabriel" & " " & totalTime1.ToString)```

Now when I try to check for each name on an array list to calculate the total time I get wrong result!What I am doing wrong?

The code

```Dim totalHours1 As Integer
Dim totalMinutes1 As Integer
Dim nn() As String = {"Anri", "Gabriel"}
For index As Integer = 0 To nn.Length - 1
For Each S As String In ListBox2.Items
If S.ToString.Contains(nn(index)) And Not S.ToString.Contains("*") Then
totalHours1 += S.Split(" "c)(3).Split(":")(0)
totalMinutes1 += S.Split(" "c)(3).Split(":")(1)
End If
Next
Dim remainder1 = totalMinutes1 Mod 60
totalHours1 += totalMinutes1 \ 60
Dim totalTime1 = totalHours1.ToString("d2") & ":" & remainder1.ToString("d2")
Next```

Tuesday, June 25, 2019 4:52 PM

• You should describe *exactly* what is wrong with the result. That will focus
attention on the specific operations rather than expecting others to build
complete programs to test your code and try to see what appears abnormal.

Show examples of values that *should* appear with examples of what actually
appears in the result.

One immediate thought: Since it "works" for one person but gives wrong results
for multiple people, check your math operations. If you are *always* adding to a
variable without clearing before starting calculations for each new person you
may get accumulating results where you don't want it.

Or are you using += when you should be using =?

- Wayne

• Marked as answer by Wednesday, June 26, 2019 7:59 AM
Tuesday, June 25, 2019 11:35 PM

### All replies

• Hello,

I can (at least I believe this is what you want) give you all the parsing, what's left is the calculations. I took your data and place it into a text file.

```Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim personName = "Gabriel Lami"
Dim fileName As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TextFile1.txt")
Dim itemsArray = File.ReadAllLines(fileName).Where(Function(line) Not line.Contains("* * * *")).ToArray()
For Each item As String In itemsArray
Dim pos = item.IndexOf(personName)
pos += (personName.Length) + 1
Dim times = item.Substring(pos).TrimEnd().Split(" "c)
For Each theTime As String In times
Dim goodTime As TimeSpan
If TimeSpan.TryParse(theTime, goodTime) Then
Console.WriteLine(goodTime)
Else
Console.WriteLine(\$"This {theTime} is not valid")
End If
Next
Console.WriteLine()
Next
End Sub```

Results

04:14:00
13:21:20
17:35:40

04:06:00
13:25:50
17:32:10

04:10:00
13:23:50
17:33:50

04:09:00
13:23:40
17:33:00

Text file, enough to get the results

Gabriel * * * * PUSHIM Gabriel * * * * PUSHIM
Gabriel * * * * PUSHIM Gabriel * * * * PUSHIM
Gabriel * * * * PUSHIM Gabriel * * * * PUSHIM
Gabriel * * * * PUSHIM Gabriel * * * * PUSHIM
Gabriel * * * * PUSHIM Gabriel * * * * PUSHIM
Gabriel * * * * PUSHIM
G/13/2019 Gabriel Lami 04:14 13:21:20 17:35:40 G/14/2019
Gabriel Lami 04:06 13:25:50 17:32:10
Gabriel * * * * PUSHIM
Gabriel * * * * PUSHIM
6/17/2019 Gabriel Lami 04:10 13:23:50 17:33:50
6/18/2019 Gabriel Lami 04:09 13:23:40 17:33:00

Gabriel * * * * PUSHIM Gabriel * * * * PUSHIM
Gabriel * * * * PUSHIM Gabriel * * * * PUSHIM
Gabriel * * * * PUSHIM Gabriel * * * * PUSHIM
Gabriel * * * * PUSHIM Gabriel * * * * PUSHIM
Gabriel * * * * PUSHIM Gabriel * * * * PUSHIM
Gabriel * * * * PUSHIM
G/13/2019 Gabriel Lami 04:14 13:21:20 17:35:40
G/14/2019 Gabriel Lami 04:06 13:25:50 17:32:10
Gabriel * * * * PUSHIM
Gabriel * * * * PUSHIM
6/17/2019 Gabriel Lami 04:10 13:23:50 17:33:50
6/18/2019 Gabriel Lami 04:09 13:23:40 17:33:00

Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

NuGet BaseConnectionLibrary for database connections.

StackOverFlow

Tuesday, June 25, 2019 6:39 PM
• I think you didn't got the idea or I didn't explained well

The data I have in the listbox is like this

```Arnold * * * * PUSHIM
Arnold * * * * PUSHIM
Arnold * * * * PUSHIM
Arnold * * * * PUSHIM
Arnold * * * * PUSHIM
Arnold * * * * PUSHIM
Arnold * * * * PUSHIM
Arnold * * * * PUSHIM
Arnold * * * * PUSHIM
Arnold * * * * PUSHIM
Arnold * * * * PUSHIM
Arnold * * * * PUSHIM
Arnold * * * * PUSHIM
Aurela * * * * PUSHIM
Aurela * * * * PUSHIM
Aurela * * * * PUSHIM
Aurela * * * * PUSHIM
Aurela * * * * PUSHIM
Aurela * * * * PUSHIM
Aurela * * * * PUSHIM
Aurela * * * * PUSHIM
Aurela * * * * PUSHIM
Aurela * * * * PUSHIM
Aurela * * * * PUSHIM
Aurela * * * * PUSHIM
6/13/2019 Aurela Llojko 08:38 08:59:10 17:37:50
6/14/2019 Aurela Llojko 08:40 08:54:50 17:35:30
Aurela * * * * PUSHIM
Aurela * * * * PUSHIM
6/17/2019 Aurela Llojko 08:21 09:10:40 17:32:20
6/18/2019 Aurela Llojko 08:41 08:54:30 17:36:00
Aurela * * * * PUSHIM
Aurela * * * * PUSHIM
Aurela * * * * PUSHIM
Aurela * * * * PUSHIM
Aurela * * * * PUSHIM
Aurela * * * * PUSHIM
Aurela * * * * PUSHIM
Aurela * * * * PUSHIM
Aurela * * * * PUSHIM
Aurela * * * * PUSHIM
Aurela * * * * PUSHIM
Aurela * * * * PUSHIM
Aurela * * * * PUSHIM
Bejane * * * * PUSHIM
Bejane * * * * PUSHIM
Bejane * * * * PUSHIM
Bejane * * * * PUSHIM
Bejane * * * * PUSHIM
Bejane * * * * PUSHIM
Bejane * * * * PUSHIM
Bejane * * * * PUSHIM
Bejane * * * * PUSHIM
Bejane * * * * PUSHIM
Bejane * * * * PUSHIM
Bejane * * * * PUSHIM
6/13/2019 Bejane Zenjelaj 08:43 08:54:30 17:38:20
6/14/2019 Bejane Zenjelaj 10:23 08:42:30 19:05:50
Bejane * * * * PUSHIM
Bejane * * * * PUSHIM
6/17/2019 Bejane Zenjelaj 09:10 08:59:10 18:09:50
6/18/2019 Bejane Zenjelaj 07:58 08:59:10 16:57:10
Bejane * * * * PUSHIM
Bejane * * * * PUSHIM
Bejane * * * * PUSHIM
Bejane * * * * PUSHIM
Bejane * * * * PUSHIM
Bejane * * * * PUSHIM
Bejane * * * * PUSHIM
Bejane * * * * PUSHIM
Bejane * * * * PUSHIM
Bejane * * * * PUSHIM
Bejane * * * * PUSHIM
Bejane * * * * PUSHIM
Bejane * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
6/13/2019 Blendi Avrami 08:30 09:00:10 17:31:00
6/14/2019 Blendi Avrami 08:41 08:51:40 17:33:20
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
6/17/2019 Blendi Avrami 08:31 09:00:50 17:32:30
6/18/2019 Blendi Avrami 08:45 08:50:00 17:35:50
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Bleona * * * * PUSHIM
Bleona * * * * PUSHIM
Bleona * * * * PUSHIM
Bleona * * * * PUSHIM
Bleona * * * * PUSHIM
Bleona * * * * PUSHIM
Bleona * * * * PUSHIM
Bleona * * * * PUSHIM
Bleona * * * * PUSHIM
Bleona * * * * PUSHIM
Bleona * * * * PUSHIM
Bleona * * * * PUSHIM```

What I want is for each name on the listbox get the time(first time) and sum it up and give the total time for each person

So

`Aurela  -> 08:38 + 08:40 + 08:21 + 08:41 = 34:20Bejane  -> 08:43 + 10:23 + 09:10 + 07:58 = 36:14Blendi -> 08:30 + 08:41 + 08:31 + 08:45 = 34:27`

Tuesday, June 25, 2019 6:54 PM
• Yes the items are in a ListBox is an array e.g.

`Dim data As String() = ListBox1.Items.Cast(Of String).ToArray()`
`dim lessData = data.Where(Function(line) Not line.Contains("* * * *")).ToArray()`

What I gave you was a pattern which you need to work on. I don't have the time to write a full example e.g. for each name you would need a loop.

Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

NuGet BaseConnectionLibrary for database connections.

StackOverFlow

Tuesday, June 25, 2019 7:00 PM
• Sorry to say but you are not getting the idea of what I want.What your code gets is not what I want and the all purpose of this thread was the array loop!

If you read my first post you will understand.I already gave the code I am using

Based on this code(witch gets from listbox2 only specific name I am able to calculate what I want) But where I am stuck is how to do that for each name(in a array)?

I don't want other code I want the code I posted to change it and get from the listbox2 not only the name "Blendi" but for each name that are in a array.If you see the code I gave and understand what it does than you will understand how to help me

```Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
6/13/2019 Blendi Avrami 08:30 09:00:10 17:31:00
6/14/2019 Blendi Avrami 08:41 08:51:40 17:33:20
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
6/17/2019 Blendi Avrami 08:31 09:00:50 17:32:30
6/18/2019 Blendi Avrami 08:45 08:50:00 17:35:50
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
Blendi * * * * PUSHIM
```

Add this code to button1 and run it and see what it does to understand what I want

```Dim totalHours As Integer
Dim totalMinutes As Integer
For Each S As String In ListBox2.Items
If S.Contains("Blendi") And Not S.Contains("*") Then
totalHours += S.Split(" "c)(3).Split(":")(0)
totalMinutes += S.Split(" "c)(3).Split(":")(1)
End If
Next
Dim remainder = totalMinutes Mod 60
totalHours += totalMinutes \ 60
Dim totalTime1 = totalHours.ToString("d2") & ":" & remainder.ToString("d2")
MSGBOX("Gabriel" & " " & totalTime1.ToString)```

• Edited by Tuesday, June 25, 2019 9:00 PM
Tuesday, June 25, 2019 8:55 PM
• You should describe *exactly* what is wrong with the result. That will focus
attention on the specific operations rather than expecting others to build
complete programs to test your code and try to see what appears abnormal.

Show examples of values that *should* appear with examples of what actually
appears in the result.

One immediate thought: Since it "works" for one person but gives wrong results
for multiple people, check your math operations. If you are *always* adding to a
variable without clearing before starting calculations for each new person you
may get accumulating results where you don't want it.

Or are you using += when you should be using =?

- Wayne

• Marked as answer by Wednesday, June 26, 2019 7:59 AM
Tuesday, June 25, 2019 11:35 PM
• I already explained and provided the code I am using and I wanted help based on my code to achieve what I was missing or I was stuck but as a reply was given another code that was doing another thing witch I didn't wanted!

@WayneAKing reading your post I was able to find the solution and fix my code!

" If you are *always* adding to a
variable without clearing before starting calculations for each new person you
may get accumulating results where you don't want it. "

Yes.I was not clearing the accumulated data for each name !

So adding this fixed my code

```totalHours1 = 0
totalMinutes1 = 0```

Final working code

``` Dim totalHours1 As Integer
Dim totalMinutes1 As Integer
Dim nn() As String = {"Bejane", "Blendi"}
For index As Integer = 0 To nn.Length - 1
For Each S As String In ListBox1.Items
If S.ToString.Contains(nn(index)) And Not S.ToString.Contains("*") Then
totalHours1 += S.Split(" "c)(3).Split(":")(0)
totalMinutes1 += S.Split(" "c)(3).Split(":")(1)

End If

Next
Dim remainder1 = totalMinutes1 Mod 60
totalHours1 += totalMinutes1 \ 60
Dim totalTime1 = totalHours1.ToString & ":" & remainder1.ToString
totalHours1 = 0
totalMinutes1 = 0