gpt4 book ai didi

powershell - PowerShell修改CSV中的日期列

转载 作者:行者123 更新时间:2023-12-02 23:54:27 25 4
gpt4 key购买 nike

我从银行收到了一系列csv报表,希望合并并提取相关行。我设法将行删除了,但是对于日期列,我想将日期从DD / MM / YYYY HH:MM重新格式化为YYYY-MM-DD,但我无法用Google来确定应该怎么做的概念这个?我是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

更新:感谢马丁,我现在得到以下内容...需要弄清楚为什么我得到错过的日期值。
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

最佳答案

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

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

关于powershell - PowerShell修改CSV中的日期列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48781580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com