Reformat value from Milliseconds to Hours:Minutes;Seconds in Select-Object?

Welcome Forums General PowerShell Q&A Reformat value from Milliseconds to Hours:Minutes;Seconds in Select-Object?

Viewing 5 reply threads
  • Author
    Posts
    • #190150
      Participant
      Topics: 4
      Replies: 5
      Points: 13
      Rank: Member

      Hi,

      I am using the following Select-Object statement and I would like to re-format the $_.duration value from milliseconds to hours:minutes:seconds?

      I have found the following gets me close; but it has the remaining milliseconds at the end of the string.

      `Select-Object @{N='Title'; E={$_.title}}, @{N="Duration H:M:S"; E={[timespan]::frommilliseconds($_.duration).ToString()}},@{N="Track ID"; E={$_.id}}`

      As an example, if $_.duration = "3215882" (so 53 Minutes, 35 seconds) it returns 00:53:35.8820000

      How do I remove/mask the remaining milliseconds?

      I would be grateful if someone could help me put this to bed; it feels so simple, but I am hitting a brick wall.

      Thank you in advance.

      M

       

    • #190165
      Participant
      Topics: 4
      Replies: 95
      Points: 192
      Helping Hand
      Rank: Participant
      3215882 | Select-Object @{N="Duration H:M:S"; E={"{0:hh}:{0:mm}:{0:ss}" -f [timespan]::frommilliseconds($_)}}
      
      Duration H:M:S
      ————–
      00:53:35
      
      
    • #190174
      Participant
      Topics: 4
      Replies: 5
      Points: 13
      Rank: Member

      Thank you so much Aapeli.

      M

    • #190198
      Participant
      Topics: 6
      Replies: 658
      Points: 47
      Rank: Member

      For future folks looking at this post, you can also just use tostring() to format it.

      $objects = @(
        [pscustomobject]@{
          title = "T1"
          duration = 3215882
          id = 123
        },
        [pscustomobject]@{
          title = "T2"
          duration = 3245643
          id = 456
        }
      )
      
      $objects |
      Select-Object @{N='Title'; E={$_.title}}, @{N="Duration H:M:S"; E={[timespan]::frommilliseconds($_.duration).tostring("hh\:mm\:ss")}},@{N="Track ID"; E={$_.id}}
      Title Duration H:M:S Track ID
      ----- -------------- --------
      T1    00:53:35       123
      T2    00:54:05       456

       

    • #190219
      Participant
      Topics: 4
      Replies: 95
      Points: 192
      Helping Hand
      Rank: Participant

      For future folks looking at this post, you can also just use tostring() to format it

      Yes! That's the way I would like to have done it too. I couldn't get my head around the separator there. I was missing the \ escape.

      Thanks for this!

    • #190387
      Participant
      Topics: 4
      Replies: 5
      Points: 13
      Rank: Member

      Thank you too Curtis.

      M

Viewing 5 reply threads
  • You must be logged in to reply to this topic.