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.
- 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:
- 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.