Paulo
Paulo

Reputation: 361

Powershel convert from milliseconds to time format

I have this code to format my result in time format.

  [TimeSpan[]]$outItems = foreach ($eachtimer in $DurationColl){
  if ($eachtimer.Contains('M')){
  Convert-TimeString -Time  $eachtimer -Format 'm\Ms\.fff\S'}
  else{
  Convert-TimeString -Time $eachtimer -Format "h\:mm\:ss"} 
  }
  ($outItems | Measure-Object -Property TotalSeconds -Sum).Sum
  $ts =  [timespan]::fromseconds($outItems)
  ("{0:hh\:mm\:ss\,fff}" -f $ts)

This is the $outItems value 14738.631

Cannot convert argument "value", with value: "System.TimeSpan[]", for "FromSeconds" to type "System.Double": "Cannot convert the 
"System.TimeSpan[]" value of type "System.TimeSpan[]" to type "System.Double"."
At line:3 char:4
+    $ts =  [timespan]::fromseconds($outItems)
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

This is the output that I get, but with the message above:

00:02:37.5260000

And I need to get something like this (hh:mm:ss.fff):

02:37.526

Can you please give me some help to understand what I'm doing wrong? Thanks for any help.

Upvotes: 1

Views: 391

Answers (1)

Mike Shepard
Mike Shepard

Reputation: 18176

How 'bout this? You were seeing the Sum property, but not capturing it.

$milliseconds=  ($outItems | Measure-Object -Property TotalSeconds -Sum).Sum
$ts =  [timespan]::fromseconds($milliseconds)
("{0:hh\:mm\:ss\,fff}" -f $ts)

Upvotes: 3

Related Questions