【问题标题】:Powershel convert from milliseconds to time formatPowershell从毫秒转换为时间格式
【发布时间】:2020-06-15 23:44:13
【问题描述】:

我有这段代码可以将我的结果格式化为时间格式。

  [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

你能帮我理解我做错了什么吗? 感谢您的帮助。

【问题讨论】:

  • 为什么你希望[timespan]::FromSeconds() 调用接受一个数组?
  • Here 您可以找到有关日期时间格式的其他信息。

标签: powershell datetime datetime-format


【解决方案1】:

这个怎么样?您看到了 Sum 属性,但没有捕获它。

$milliseconds=  ($outItems | Measure-Object -Property TotalSeconds -Sum).Sum
$ts =  [timespan]::fromseconds($milliseconds)
("{0:hh\:mm\:ss\,fff}" -f $ts)

【讨论】:

    猜你喜欢
    • 2020-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-17
    相关资源
    最近更新 更多