PowerShell中的时间格式

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 
} 
+0

的[给定DateTime对象可能的复制,我怎么弄字符串格式的ISO 8601日期?](https://*.com/questions/114983/given-a-datetime-object-how-do-i-get-an-iso-8601-date-in-string-格式) – Tomalak

+0

另一个重复。 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

+0

得到它 (GET-ComputerRestorePoint).CreationTime,(获取最新)的ToString( 'YYYY-MM-DD') 但后来它还是输出原始TIMEFORMAT 20171021104047.441419-000 2017-10 -21 – IIIdefconIII

可以使用-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"}} 
+0

谢谢你,但它没有工作:仍然得到时间不可读格式 20171021104047.441419-000 '' '(Get-ComputerRestorePoint).CreationTime | Format-Table @ {Label =“Date”; Expression = {“{0:MM/dd/yyyy HH:mm}”-f $ _。ConvertToDateTime($ _。CreationTime)}}' – IIIdefconIII

+0

'Get-ComputerRestorePoint | Format-Table ...',而不是'(Get-ComputerRestorePoint).CreationTime | ...' –

+0

不好意思,冷静这个原因信息是在桌子上,得到它,但我仍然有一张桌子,我想这个平坦的地方甚至可能吗? ty – IIIdefconIII