简体   繁体   English

Powershell 转换时间

[英]Powershell convert time

I'm trying to convert a time with format ss.ff我正在尝试使用 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 $ts 变量的结果

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谢谢你的帮助

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.您会注意到它们都没有 3 个参数,这也是您的错误明确说明的内容。 You can also find the info here: https://docs.microsoft.com/en-us/dotnet/api/system.timespan.parseexact?view=netcore-3.1您还可以在此处找到信息: https : //docs.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.在这种情况下, $ts看起来与上面的输出完全一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM