Powershell将持续时间字符串解析为秒

use*_*851 1 powershell powershell-2.0

我有一个这样的字符串:"00:02:37.6940000".有没有一种简单的方法可以将其转换/解析为秒?我是否必须将它拼凑成片并按照这种方式进行操作?

我不关心毫秒.

mjo*_*nor 10

Parse是[TimeSpan]类型的默认方法,因此:

([timespan]"00:02:37.6940000").TotalSeconds
Run Code Online (Sandbox Code Playgroud)

也应该工作.

使用错误捕获:

$input_ts = "00:02:37.6940000" 

if ($input_ts -as [TimeSpan])
  {$time = ([TimeSpan]$input_ts).TotalSeconds}

else {Write-Warning "Input value $input_ts not valid for timespan"}
Run Code Online (Sandbox Code Playgroud)