2017-10-21 98 views
1

我的同事stackoverflow同事,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://stackoverflow.com/questions/114983/given-a-datetime-object-how-do-i-get-an-iso-8601-date-in-string-格式) – Tomalak

+0

另一個重複。 https://stackoverflow.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

回答

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