Welcome › Forums › General PowerShell Q&A › Reformat value from Milliseconds to Hours:Minutes;Seconds in Select-Object?
- This topic has 5 replies, 3 voices, and was last updated 2 weeks, 1 day ago by
Participant.
-
AuthorPosts
-
-
November 23, 2019 at 4:32 pm #190150
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
-
November 23, 2019 at 6:13 pm #190165
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
-
November 23, 2019 at 8:47 pm #190174
Thank you so much Aapeli.
M
-
November 23, 2019 at 11:45 pm #190198
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
-
November 24, 2019 at 6:53 am #190219
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!
-
November 25, 2019 at 5:51 pm #190387
Thank you too Curtis.
M
-
-
AuthorPosts
- You must be logged in to reply to this topic.