我无法使用 Powershell 获取某个日期的时差,如下所示:
$Date = '11/12/2001'
(New-TimeSpan -Start (Get-Date -Date $Date) -End (Get-Date)).ToString("'yyyy' Years 'MM' Months 'dd' Days 'hh' Hours 'mm' Minutes 'ss' Seconds'")
错误:
Exception calling "ToString" with "1" argument(s): "Input string was not in a correct format."
At line:2 char:1
+ (New-TimeSpan -Start (Get-Date -Date $Date) -End (Get-Date)).ToString ...
为什么我无法获得过去的年份和月份?
答案1
TimeSpan 没有年份或月份。
$Date = "11/12/2001"
$TimeSpan = (New-TimeSpan -Start (Get-Date -Date $Date)
Write-Output "$($TimeSpan.ToString("dddd")) Days $($TimeSpan.ToString("hh")) Hours $($TimeSpan.ToString("mm")) Minutes $($TimeSpan.ToString("ss")) Seconds"
6871 天 10 小时 30 分钟 18 秒