0

I am currently writing a PowerShell Script which basically reads a file where it searches for a pattern and gets time Values like: 15:22:33. The Values are stored in 2 Arrays - Out Array and In Array that both hold the same type of values.

So I am subtracting the 2 values and I am creating a timespan that for example calcs the time between this two values.

  1. 11:20:33 and 2. 12:21:33 - so the timespan correctly shows me that the difference is 01:01:00 which is correct ofc.

Now my problem is that I also need to calculate the difference if the next date is tomorrow.

So take this example:

  1. 12:00:00 and 2. 05:00:00 - if I use the same function I do above, so just subtract the values I get -07:00:00 but the value I need would be 17:00:00.

How would a math function look like to calculate the value for next day? I can check it with an IF Statement because if the second value is smaller that the first one its obviously the next day. But I really don't know how to calc the time correctly.

Some code:

for($i = 0; $i -lt $outVar.Length; $i++) {
    $testOut = $outVar[$i].Line.Split("(")[0]
    $testIn  = $inVar[$i].Line.Split("(")[0]

    $userOut = $outVar[$i].Line.Split('"')[2]
    $userIn = $inVar[$i].Line.Split('"')[2]

    #$testOut + $testIn                                          
    $ConvertedStart = [datetime]::Parse($testOut)
    $ConvertedEnd = [datetime]::Parse($testIn)

    $loggedTimeTestSTART = New-TimeSpan  $ConvertedStart
    $loggedTimeTestEND = New-TimeSpan  $ConvertedEnd

    [System.Collections.ArrayList]$resultArray = @();            


    
    if($loggedTimeTestSTART -gt $loggedTimeTestEND) {
        $loggedTimeTestFinal = $loggedTimeTestSTART.Subtract($loggedTimeTestEND)
       # $resultArray.Add($loggedTimeTestFinal)
        #$resultArray += $loggedTimeTestFinal
        $array += $loggedTimeTestFinal.ToString() + $userOut
        } else {
            $loggedTimeTestFinal = "Minus cuz next day " + $loggedTimeTestSTART.Subtract($loggedTimeTestEND) 
           # $resultArray.Add($loggedTimeTestFinal);
            #$resultArray += $loggedTimeTestFinal 
            $array += $loggedTimeTestFinal.ToString() + $userOut
        }
    
    }

as you can see in my If the else part would need to do some different calculation to get the real time difference (timespan) between two values if the second one is smaller.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
yesIamFaded
  • 1,970
  • 2
  • 20
  • 45
  • 1
    btw, you should try to [avoid using the increase assignment operator (+=) to create a collection](https://stackoverflow.com/questions/60708578/why-should-i-avoid-using-the-increase-assignment-operator-to-create-a-colle/60708579#60708579) – iRon Apr 08 '20 at 11:06

1 Answers1

3

Keep the first item ($a) as a [TimeSpan] and convert the second ($b) to [DateTime]. Than subtract the [TimeSpan] from the [DateTime] and select the TimeOfDay part to get the time difference:

$a = [TimeSpan]'11:20:33'
$b = [DateTime]'12:21:33'
($b - $a).TimeOfDay

yields:

Days              : 0
Hours             : 1
Minutes           : 1
Seconds           : 0
Milliseconds      : 0
Ticks             : 36600000000
TotalDays         : 0.0423611111111111
TotalHours        : 1.01666666666667
TotalMinutes      : 61
TotalSeconds      : 3660
TotalMilliseconds : 3660000

 

$a = [TimeSpan]'12:00:00'
$b = [DateTime]'05:00:00'
($b - $a).TimeOfDay

yields:

Days              : 0
Hours             : 17
Minutes           : 0
Seconds           : 0
Milliseconds      : 0
Ticks             : 612000000000
TotalDays         : 0.708333333333333
TotalHours        : 17
TotalMinutes      : 1020
TotalSeconds      : 61200
TotalMilliseconds : 61200000

Alternatively, you might just add one day, if the result is less then 0:

$a = [TimeSpan]'12:00:00'
$b = [TimeSpan]'05:00:00'
$Result = $b - $a
If ($Result -lt 0) {$Result += [TimeSpan]'1:00:00:00'}
iRon
  • 20,463
  • 10
  • 53
  • 79
  • so the second one needs to be a datetime? or should i make both a datetime ? or is it just the .TimeOfDay that does the trick? - thanks – yesIamFaded Apr 08 '20 at 08:32
  • Alternatively, you might just add one day, if the result is less then `0` (see update). – iRon Apr 08 '20 at 08:45
  • again thanks for the response I changed my ELSE to this but I still get -08:04:21 for this: else { $loggedTimeTestFinal = "Minus cuz next day " + $loggedTimeTestSTART.Subtract($loggedTimeTestEND) $nextDayFinal = $loggedTimeTestFinal += [timespan]'01:00:00' $array += $nextDayFinal + $userOut } – yesIamFaded Apr 08 '20 at 08:57