在PowerShell中将秒转换为hh:mm:ss,fff格式

时间:2022-06-28 02:49:23

I have a string representing a time in seconds and milliseconds. I want to convert it to a string in the format "hh:mm:ss,fff".

我有一个字符串表示以秒和毫秒为单位的时间。我想将其转换为格式为“hh:mm:ss,fff”的字符串。

My solution still has the flaw that hours less than 10 are shown with one decimal instead of two:

我的解决方案仍有缺陷,小于10的小时数显示为一个小数而不是两个:

PS> $secs = "7000.6789"
PS> $ts =  [timespan]::fromseconds($s)
PS> $res = "$($ts.hours):$($ts.minutes):$($ts.seconds),$($ts.milliseconds)"
PS> $res
PS> 1:56:40,679

What is the right way to achieve this? I'm sure there is a more elegant way with -f and datetime.

实现这一目标的正确方法是什么?我确信-f和datetime有更优雅的方式。

3 个解决方案

#1


22  

In PowerShell 4.0

在PowerShell 4.0中

$s = "7000.6789"
$ts =  [timespan]::fromseconds($s)
("{0:HH\:mm\:ss\,fff}" -f $ts)

Output: 01:56:40,679

输出:01:56:40,679


In PowerShell 2.0

在PowerShell 2.0中

$s = "7000.6789"
$ts =  [timespan]::fromseconds($s)
"{0:HH:mm:ss,fff}" -f ([datetime]$ts.Ticks)

Output: 01:56:40,679

输出:01:56:40,679


And to go back the other way...

而要回到另一个方向......

$text = "01:56:40,679"
$textReformat = $text -replace ",","."
$seconds = ([TimeSpan]::Parse($textReformat)).TotalSeconds
$seconds

Output: 7000.679

产量:7000.679

#2


7  

You could just use the ToString method on the TimeSpan object and specify the format you want to use. Either use one of the standard timespan formats or use a custom timespan format. For example, the following custom format gives the output you want:

您可以在TimeSpan对象上使用ToString方法并指定要使用的格式。使用标准时间跨度格式之一或使用自定义时间跨度格式。例如,以下自定义格式提供您想要的输出:

$ts =  [timespan]::fromseconds("7000.6789")
$ts.ToString("hh\:mm\:ss\,fff")

This will output

这将输出

01:56:40,679

Update: Updating to provide functions working in PowerShell v2

更新:更新以提供在PowerShell v2中工作的功能

The above solution works well in PowerShell v4, but not in v2 (since the TimeSpan.ToString(string) method wasn't added until .NET Framework 4).

上述解决方案在PowerShell v4中运行良好,但在v2中运行不正确(因为在.NET Framework 4之前未添加TimeSpan.ToString(字符串)方法)。

In v2 I guess you'll have to either create the string manually (like you are doing in the question) or doing an ordinary ToString() and manipulate the string. I suggest the former. Here's a function which works fine for that:

在v2中我想你必须手动创建字符串(就像你在问题中做的那样)或者做一个普通的ToString()并操纵字符串。我建议前者。这是一个适用于此的功能:

function Format-TimeSpan
{
    PARAM (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [TimeSpan]$TimeSpan
    )

    #Current implementation doesn't handle days.

    #By including the delimiters in the formatting string it's easier when we contatenate in the end
    $hours = $TimeSpan.Hours.ToString("00")
    $minutes = $TimeSpan.Minutes.ToString("\:00")
    $seconds = $TimeSpan.Seconds.ToString("\:00")
    $milliseconds = $TimeSpan.Milliseconds.ToString("\,000")

    Write-Output ($hours + $minutes + $seconds + $milliseconds)
}

Testing it using

使用它进行测试

$ts =  [timespan]::fromseconds("7000.6789")

Format-TimeSpan -TimeSpan $ts
$ts | Format-TimeSpan

Yields the following output:

产生以下输出:

01:56:40,679
01:56:40,679

#3


1  

One line conversion :

一线转换:

[timespan]::fromseconds(354801857.86437).tostring()

return 4106.12:04:17.8640000

返回4106.12:04:17.8640000

#1


22  

In PowerShell 4.0

在PowerShell 4.0中

$s = "7000.6789"
$ts =  [timespan]::fromseconds($s)
("{0:HH\:mm\:ss\,fff}" -f $ts)

Output: 01:56:40,679

输出:01:56:40,679


In PowerShell 2.0

在PowerShell 2.0中

$s = "7000.6789"
$ts =  [timespan]::fromseconds($s)
"{0:HH:mm:ss,fff}" -f ([datetime]$ts.Ticks)

Output: 01:56:40,679

输出:01:56:40,679


And to go back the other way...

而要回到另一个方向......

$text = "01:56:40,679"
$textReformat = $text -replace ",","."
$seconds = ([TimeSpan]::Parse($textReformat)).TotalSeconds
$seconds

Output: 7000.679

产量:7000.679

#2


7  

You could just use the ToString method on the TimeSpan object and specify the format you want to use. Either use one of the standard timespan formats or use a custom timespan format. For example, the following custom format gives the output you want:

您可以在TimeSpan对象上使用ToString方法并指定要使用的格式。使用标准时间跨度格式之一或使用自定义时间跨度格式。例如,以下自定义格式提供您想要的输出:

$ts =  [timespan]::fromseconds("7000.6789")
$ts.ToString("hh\:mm\:ss\,fff")

This will output

这将输出

01:56:40,679

Update: Updating to provide functions working in PowerShell v2

更新:更新以提供在PowerShell v2中工作的功能

The above solution works well in PowerShell v4, but not in v2 (since the TimeSpan.ToString(string) method wasn't added until .NET Framework 4).

上述解决方案在PowerShell v4中运行良好,但在v2中运行不正确(因为在.NET Framework 4之前未添加TimeSpan.ToString(字符串)方法)。

In v2 I guess you'll have to either create the string manually (like you are doing in the question) or doing an ordinary ToString() and manipulate the string. I suggest the former. Here's a function which works fine for that:

在v2中我想你必须手动创建字符串(就像你在问题中做的那样)或者做一个普通的ToString()并操纵字符串。我建议前者。这是一个适用于此的功能:

function Format-TimeSpan
{
    PARAM (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [TimeSpan]$TimeSpan
    )

    #Current implementation doesn't handle days.

    #By including the delimiters in the formatting string it's easier when we contatenate in the end
    $hours = $TimeSpan.Hours.ToString("00")
    $minutes = $TimeSpan.Minutes.ToString("\:00")
    $seconds = $TimeSpan.Seconds.ToString("\:00")
    $milliseconds = $TimeSpan.Milliseconds.ToString("\,000")

    Write-Output ($hours + $minutes + $seconds + $milliseconds)
}

Testing it using

使用它进行测试

$ts =  [timespan]::fromseconds("7000.6789")

Format-TimeSpan -TimeSpan $ts
$ts | Format-TimeSpan

Yields the following output:

产生以下输出:

01:56:40,679
01:56:40,679

#3


1  

One line conversion :

一线转换:

[timespan]::fromseconds(354801857.86437).tostring()

return 4106.12:04:17.8640000

返回4106.12:04:17.8640000