PowerShell中的时间格式
我的同事*同事,PowerShell中的时间格式
我喜欢有以下PowerShell脚本输出没有表中的日期部分。
10/21/2017 16:49 | Systrax Restore Point
目前的输出是:
@{Date=10/21/2017 12:40} | Systrax Auto Restore Point
这是我的脚本的一部分:
$creationtime = Get-ComputerRestorePoint | Select-Object @{Label="Date"; Expression={"{0:MM/dd/yyyy HH:mm}" -f $_.ConvertToDateTime($_.CreationTime)}}
$restorepoint = (Get-ComputerRestorePoint).Description
if ($restorepoint -eq $null){
Enable-ComputerRestore -Drive "C:\"
Checkpoint-Computer "Systrax Auto Restore Point"
Write-Host "No restore points, Auto Creating..."
Exit 1010
}
else {
Write-Host "$creationtime | $restorepoint"
Exit 0
}
可以使用-f
格式运算符来格式化结果DateTime
对象您的计算属性:
... |Format-Table @{Label="Date"; Expression={"{0:MM/dd/yyyy HH:mm}" -f $_.ConvertToDateTime($_.CreationTime)}}
另一种选择是调用对象的ToString()
:
... |Format-Table @{Label="Date"; Expression={$_.ConvertToDateTime($_.CreationTime).ToString("MM/dd/yyyy HH:mm")}}
或者你也可以有Get-Date
为你做:
... |Format-Table @{Label="Date"; Expression={$_.ConvertToDateTime($_.CreationTime) |Get-Date -Format "MM/dd/yyyy HH:mm"}}
谢谢你,但它没有工作:仍然得到时间不可读格式 20171021104047.441419-000 '' '(Get-ComputerRestorePoint).CreationTime | Format-Table @ {Label =“Date”; Expression = {“{0:MM/dd/yyyy HH:mm}”-f $ _。ConvertToDateTime($ _。CreationTime)}}' – IIIdefconIII
'Get-ComputerRestorePoint | Format-Table ...',而不是'(Get-ComputerRestorePoint).CreationTime | ...' –
不好意思,冷静这个原因信息是在桌子上,得到它,但我仍然有一张桌子,我想这个平坦的地方甚至可能吗? ty – IIIdefconIII
的[给定DateTime对象可能的复制,我怎么弄字符串格式的ISO 8601日期?](https://*.com/questions/114983/given-a-datetime-object-how-do-i-get-an-iso-8601-date-in-string-格式) – Tomalak
另一个重复。 https://*.com/questions/2249619/how-to-format-a-datetime-in-powershell,其中许多其他人。请在提问前进行搜索。相关文件。 https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings(此外,'mm/dd/yyyy'不是一个明智的日期格式,避免。) – Tomalak
得到它 (GET-ComputerRestorePoint).CreationTime,(获取最新)的ToString( 'YYYY-MM-DD') 但后来它还是输出原始TIMEFORMAT 20171021104047.441419-000 2017-10 -21 – IIIdefconIII