Reputation: 105
I'm not sure if this is going to be a simple answer or not. I tried searching for ways to convert ISO times online and I believe ISO 8601 is the time format I'm dealing with but I'm not 100% sure, and I couldn't find much info about it.
To keep it super short, in an XML document I'm parsing in Powershell, there is a duration field written as "PT8M51.693S" which is equal to 8:51.693, or 8 minutes and 51.693 seconds. I would like to convert it to "8:51" or "08:51". Anyone have any idea how I would do this? Thanks for any help.
Upvotes: 1
Views: 1037
Reputation: 22102
You can use [Xml.XmlConvert]::ToTimeSpan
to convert from XML duration type to .NET TimeSpan
type. And for converting TimeSpan
object to string representation in desired format, you can use PowerShell format operator -f
:
$ts = [Xml.XmlConvert]::ToTimeSpan("PT8M51.693S")
'{0:00}:{1:00}:{2:00}' -f ($ts.Hours+$ts.Days*24), $ts.Minutes, $ts.Seconds
Upvotes: 6