Paulo
Paulo

Reputation: 361

Powershell convert time

I'm trying to convert a time with format ss.ff

I've got the conversion but gives me the follow error:

Cannot find an overload for "TryParseExact" and the argument count: "3".
At line:1 char:6
+      $ts = [TimeSpan]::TryParseExact('28.50 s', "ss\.ff\ s", [Culture ...
+      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest 

My code:

 $ts = [TimeSpan]::TryParseExact('28.50 s', "ss\.ff\ s", [CultureInfo]::InvariantCulture)
("{0:hh\:mm\:ss}" -f $ts)

result of $ts variable

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 28
Milliseconds      : 500
Ticks             : 285000000
TotalDays         : 0.000329861111111111
TotalHours        : 0.00791666666666667
TotalMinutes      : 0.475
TotalSeconds      : 28.5
TotalMilliseconds : 28500

What i'm doing wrong?

Thanks for any help

Upvotes: 0

Views: 342

Answers (1)

PMental
PMental

Reputation: 1179

If you type

[timespan]::TryParseExact

and press enter, you'll see all valid Overloads. You'll notice none of them have 3 arguments, which is also what your error clearly states. You can also find the info here: https://learn.microsoft.com/en-us/dotnet/api/system.timespan.parseexact?view=netcore-3.1

So basically your syntax is incorrect.

Is perhaps this what you're after?

$ts = [timespan]::ParseExact('28.50 s','ss\.ff\ \s',[cultureinfo]::InvariantCulture)
("{0:hh\:mm\:ss}" -f $ts)

In this case $ts looks exactly like your output above.

Upvotes: 1

Related Questions