Powershell从毫秒转换为时间格式
Powershel convert from milliseconds to time format
我有这段代码可以将我的结果格式化为时间格式。
[TimeSpan[]]$outItems = foreach ($eachtimer in $DurationColl){
if ($eachtimer.Contains('M')){
Convert-TimeString -Time $eachtimer -Format 'm\Ms\.fff\S'}
else{
Convert-TimeString -Time $eachtimer -Format "h\:mm\:ss"}
}
($outItems | Measure-Object -Property TotalSeconds -Sum).Sum
$ts = [timespan]::fromseconds($outItems)
("{0:hh\:mm\:ss\,fff}" -f $ts)
这是 $outItems 值
14738.631
Cannot convert argument "value", with value: "System.TimeSpan[]", for "FromSeconds" to type "System.Double": "Cannot convert the
"System.TimeSpan[]" value of type "System.TimeSpan[]" to type "System.Double"."
At line:3 char:4
+ $ts = [timespan]::fromseconds($outItems)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
这是我得到的输出,但带有上面的消息:
00:02:37.5260000
我需要得到这样的东西 (hh:mm:ss.fff):
02:37.526
能否请您帮助我了解我做错了什么?
感谢您的帮助。
这个怎么样?您看到了总和 属性,但没有捕捉到它。
$milliseconds= ($outItems | Measure-Object -Property TotalSeconds -Sum).Sum
$ts = [timespan]::fromseconds($milliseconds)
("{0:hh\:mm\:ss\,fff}" -f $ts)
我有这段代码可以将我的结果格式化为时间格式。
[TimeSpan[]]$outItems = foreach ($eachtimer in $DurationColl){
if ($eachtimer.Contains('M')){
Convert-TimeString -Time $eachtimer -Format 'm\Ms\.fff\S'}
else{
Convert-TimeString -Time $eachtimer -Format "h\:mm\:ss"}
}
($outItems | Measure-Object -Property TotalSeconds -Sum).Sum
$ts = [timespan]::fromseconds($outItems)
("{0:hh\:mm\:ss\,fff}" -f $ts)
这是 $outItems 值 14738.631
Cannot convert argument "value", with value: "System.TimeSpan[]", for "FromSeconds" to type "System.Double": "Cannot convert the "System.TimeSpan[]" value of type "System.TimeSpan[]" to type "System.Double"." At line:3 char:4 + $ts = [timespan]::fromseconds($outItems) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodException + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
这是我得到的输出,但带有上面的消息:
00:02:37.5260000
我需要得到这样的东西 (hh:mm:ss.fff):
02:37.526
能否请您帮助我了解我做错了什么? 感谢您的帮助。
这个怎么样?您看到了总和 属性,但没有捕捉到它。
$milliseconds= ($outItems | Measure-Object -Property TotalSeconds -Sum).Sum
$ts = [timespan]::fromseconds($milliseconds)
("{0:hh\:mm\:ss\,fff}" -f $ts)