3
我会尽力准确地描述这一点。我的CSV格式为:如何从DateTime删除时间CSV格式
email, DayMade
[email protected], 2013-07-30T15:31:11.000Z
[email protected], 3013-07-31T15:31:15.0001
该报告会自动生成每隔几天。我需要过滤它以删除超过一周的所有条目。我被告知要使用命令:被列入
$CutoffDate = (Get-Date).AddDays(-5)
$Data = Import-CSV "C:\gam\newestreport.csv" | `
Where-Object {$_.Date -as '[DayMade]' -lt $CutoffDate}
然而,这是返回错误,我相信通过时间(11.0007:31 T15)引起的。
我不知道如何在离开日期的同时删除时间。
'[DateTime] $ _。DayMade'也可以工作,可能会更好一点。如果他想比较没有时间的日期,他可以使用'([DateTime] $ _。DayMade).Date'并设置'$ CutoffDate =(Get-Date).Date.AddDays(-5)'。 –
@AnsgarWiechers,好点。请注意,当字符串值无效时,显式转换为DateTime将引发异常,并且使用“as”运算符进行投射将不会返回null(无双精度)。 $ null -lt $ CutoffDate将返回true,因此在DayMade中带有无效字符串的记录将使用“as”运算符返回,并且将使用显式转换运算符抛出异常。 – dugas