1

我有一个这样的字符串:“00:02:37.6940000”。有没有一种简单的方法可以将其转换/解析为几秒钟?我是否必须将它正则表达式分成几部分并这样做?

我不在乎毫秒。

4

2 回答 2

12

Parse is the default method of the [TimeSpan] type, so:

([timespan]"00:02:37.6940000").TotalSeconds

should work, too.

With error trapping:

$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"}
于 2013-04-16T17:20:07.510 回答
1

这是您可以使用的时间跨度对象的格式

[Timespan]::Parse("00:02:37.6940000")
于 2013-04-16T16:55:34.990 回答