user1612851
user1612851

Reputation: 1214

Powershell parse duration string into seconds

I have a string like this: "00:02:37.6940000". Is there an easy way to convert/parse that into seconds? Do I have to regex it into pieces and do it that way?

I don't care about the milliseconds.

Upvotes: 1

Views: 6250

Answers (2)

mjolinor
mjolinor

Reputation: 68341

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"}

Upvotes: 14

rerun
rerun

Reputation: 25505

This is the format of of timespan object you can use

[Timespan]::Parse("00:02:37.6940000")

Upvotes: 2

Related Questions