【发布时间】:2020-04-25 20:10:20
【问题描述】:
我一直在寻找类似于.NET examples 中的示例的 PowerShell 脚本。取一个 New-TimeSpan 并显示为 1 天 2 小时 3 分钟 4 秒。排除它的零,在需要的地方添加复数“s”。有谁有这个方便吗?
据我所知:
$StartDate = (Get-Date).ToString("M/dd/yyyy h:mm:ss tt")
Write-Host "Start Time: $StartDate"
# Do Something
$EndDate = (Get-Date).ToString("M/dd/yyyy h:mm:ss tt")
Write-Host "End Time: $EndDate" -ForegroundColor Cyan
$Duration = New-TimeSpan -Start $StartDate -End $EndDate
$d = $Duration.Days; $h = $Duration.Hours; $m = $Duration.Minutes; $s = $Duration.Seconds
Write-Host "Duration: $d Days, $h Hours, $m Minutes, $s Seconds"
【问题讨论】:
标签: powershell formatting timespan