Paulo
Paulo

Reputation: 361

Powershell timespan sum with results greather than 24 hours

So, i need some help to solve a little issue with the sum results of a timespan.

I've got this code to sum the results in time of two variables.

    $AllDuration = $Duration1 + $Duration2
    $Result3 = ($AllDuration | Measure-Object -Property TotalMilliseconds -Sum).Sum
    $tts =  [timespan]::FromMilliseconds($Result3)
    $Allt =("{0:hh\:mm\:ss\,fff}" -f $tts) 
    [pscustomobject]@{'Timespan' = $Allt} 

Where $Duration1 = 09:17:08,000 and $Duration2 = 09:56:53,000 (Hour:Minute:Second,Millisecond) $AllDuration = 19:14:01,000 (Sum of $Duration1 and $Duration2 )

The problem is: When the sum is greater than 24 Hours the results is the hours exceeded after 24 h.

Ex : 19:00:00,000 + 10:00:00,000 >>> Should be 29:00:00,000, but instead, the results presented is 05:00:00,000

How can i solve this to appear the right result?

Thanks for any help!

Upvotes: 0

Views: 436

Answers (1)

zdan
zdan

Reputation: 29450

The TimeSpan object splits up the timespan into Days, hours, Minutes, Seconds and Milliseconds. So you are just omitting the days portion in your format specification. You can display the days by using the d specifier:

$Allt =("{0:d\:hh\:mm\:ss\,fff}" -f $tts)

If you want to just display the total # of hours, you will have to compute it yourself:

$Allt =("{0}:{1:mm\:ss\,fff}" -f ($tts.Days*24 + $tts.Hours),$tts)

Upvotes: 1

Related Questions