PowerShell修改csv中的日期列

时间:2018-02-14 07:13:09

标签: powershell

我有一系列来自银行的csv声明,我想将它们合并并提取相关的行。我已设法将行排除,但是对于日期列,我想将日期从DD / MM / YYYY HH:MM重新格式化为YYYY-MM-DD,我不能谷歌我应该怎么做的概念这个?我是PS菜鸟。

有人能引导我朝着正确的方向前进吗?

这是我到目前为止所拥有的:

$files = Get-ChildItem -Filter *.csv 

$output = ForEach ( $file in $files ) {
    Import-CSV $file | Where-Object{$_.Description -eq "Funding Bacs" -or $_.Description -eq "Lender Withdrawal Request" } | Select-Object Date,Description,"Paid In", "Paid Out" 
    # foreach of the date fields, take the date object? string? remove the time, and reformat the date to YYYY-MM-DD 
} 

$output | Export-Csv Newtest.csv -NoTypeInformation -encoding "unicode"

当前输出:

Date             Description               Paid In Paid Out
----             -----------               ------- --------
03/04/2012 09:15 Funding Bacs              50.0    0.0
06/04/2013 17:32 Lender Withdrawal Request 0.0     234.5
01/04/2014 05:31 Funding Bacs              125.0   0.0
01/04/2014 05:31 Funding Bacs              10.0    0.0

期望的输出:

Date             Description               Paid In Paid Out
----             -----------               ------- --------
2012-04-03       Funding Bacs              50.0    0.0
2013-04-06       Lender Withdrawal Request 0.0     234.5
2014-04-01       Funding Bacs              125.0   0.0
2014-04-01       Funding Bacs              10.0    0.0

更新:感谢Martin我现在得到以下内容......需要弄清楚为什么我会得到错过的日期值。

Date             Description               Paid In Paid Out
----             -----------               ------- --------
2014-03-11 Funding Bacs              10.0    0.0
2014-03-11 Funding Bacs              125.0   0.0
           Lender Withdrawal Request 0.0     521.05
2016-07-11 Lender Withdrawal Request 0.0     188.93
           Lender Withdrawal Request 0.0     185.57
2012-03-10 Lender Withdrawal Request 0.0     148.72
2013-01-10 Funding Bacs              460.0   0.0

1 个答案:

答案 0 :(得分:1)

您可以使用计算属性格式化日期。只需将Select-Object更改为:

即可
Select-Object @{l="Date"; e={(Get-Date $_.Date).ToString('yyyy-MM-dd')}},Description,"Paid In", "Paid Out"