-1

Hello I am new with datetimes in VB net and I am trying to calculate time for some operations in productions. For example:

starTtime="01.12.2018 07:00" 
endTime ="01.12.2018 15:00"

I have example two periods without working:

Pause1Start="01.12.2018 10:00" Pause1Stop="01.12.2018 10:30"

Pause2Start="01.12.2018 14:00" Pause2Stop="01.12.2018 14:30"

How to calculate used time for operation? It is not always this example, it is necessary to check is time for pause is included between two datetime or if there is end of working day, weekends, etc... So operation can start in one day and finish day after.

I need idea how to work with this?

Thanks

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
Fikri
  • 5
  • 1
  • 5

1 Answers1

0

Try this code. I suggest you to define lists of pause startings and endings, then loop through them, check if particular pause was during the working time and add that time in seconds to variable holding total seconds:

Sub Main()
    Dim startTime = New DateTime(2018, 11, 1, 7, 0, 0)
    Dim endTime = New DateTime(2018, 11, 1, 17, 0, 0)

    Dim pauseStarts = New List(Of DateTime)
    pauseStarts.Add(New DateTime(2018, 11, 1, 16, 0, 0))
    pauseStarts.Add(New DateTime(2018, 11, 1, 10, 0, 0))
    Dim pauseEnds = New List(Of DateTime)
    ' Add respective pause endings
    pauseEnds.Add(New DateTime(2018, 11, 1, 18, 0, 0))
    pauseEnds.Add(New DateTime(2018, 11, 1, 11, 30, 0))

    Dim pauseSeconds As Long = 0
    For i = 0 To pauseStarts.Count - 1
        If pauseStarts(i) < endTime AndAlso pauseEnds(i) > startTime Then
            ' get the minimum from two times
            Dim starting = If(startTime > pauseStarts(i), startTime, pauseStarts(i))
            ' get maximum from two times
            Dim ending = If(endTime < pauseEnds(i), endTime, pauseEnds(i))
            ' add seconds during pause
            pauseSeconds += (ending - starting).TotalSeconds
        End If
    Next
End Sub
Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69