简体   繁体   English

Powershell 转换为时间

[英]Powershell Convert to time

I need to convert some particular string on time format.我需要按时间格式转换一些特定的字符串。

2M37.526S - This represents 2 minutes, 37 seconds and 526 milliseconds. 2M37.526S - 这代表 2 分 37 秒和 526 毫秒。

I need to convert on "{0:hh:mm:ss\,fff}"我需要在“{0:hh:mm:ss\,fff}”上进行转换

How the best way to do this?如何做到这一点的最佳方法? My code converts but is returning wrong order values.我的代码转换但返回错误的订单值。

My code:我的代码:

     $a = '2M37.526S' -replace '[mM]',':' -replace '[.]', ':' -replace '[sS]', ''
     $ts =  [timespan]::fromseconds($a)
     ("{0:hh\:mm\:ss\,fff}" -f $ts)

Thanks for any help谢谢你的帮助

I don't think you need to do any of the replacing.我认为您不需要进行任何更换。 Just use ParseExact, and provide the format只需使用 ParseExact,并提供格式

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

When your TimeSpan is parsed correctly, your formatting code should work properly.当您的TimeSpan被正确解析时,您的格式化代码应该可以正常工作。

$ts = [TimeSpan]::ParseExact('2M37.526S', "m\Mss\.fff\S", [CultureInfo]::InvariantCulture)
("{0:hh\:mm\:ss\,fff}" -f $ts)

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

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