简体   繁体   English

在 Powershell 中从 CSV 导入数据以 CSV 格式提供英国日期不到 7 天

[英]Importing Data From CSV in Powershell Providing UK Date in CSV is Less Than 7 Days Away

so I've got a CSV file with 5 headings: name, collectionDate, location, equipmentNotes, manager.所以我有一个带有 5 个标题的 CSV 文件:名称、集合日期、位置、设备注释、经理。

Now my collectionDates are all in UK format so DD/MM/YYYY.现在我的 collectionDates 都是英国格式,所以 DD/MM/YYYY。 I only want to import data from the CSV with collection dates that are 7 days or less in the future.我只想从 CSV 导入数据,其收集日期为未来 7 天或更短。 So today is 17/08, therefore I would only want to pull data from the CSV that is dated between 17/08 to 24/08.所以今天是 17/08,因此我只想从 17/08 到 24/08 之间的 CSV 中提取数据。

Although Powershell sort of handles datetime objects in UK format if you tell it to, I seem to be unable to then manipulate the date to add on 7 days.尽管如果你告诉它,Powershell 可以处理英国格式的日期时间对象,但我似乎无法操纵日期以添加 7 天。

Here is my current code:这是我当前的代码:

$today = Get-Date
$thisWeek = $today.AddDays(7)
$thisWeekString = $thisWeek.ToString()
$dateParts = $thisweekString -split " "
$weekFinal = $dateParts[0]

$import = Import-Csv @("\\location\EmailCSV.csv") | Where-Object {$_.collectionDate -lt $weekFinal}

Powershell correctly adds 7 days to the datetime to make it 24/08, and then when converting it to a string and removing the time from it, it correctly sets the variable as 24/08/2018. Powershell 正确地将 7 天添加到日期时间以使其成为 24/08,然后将其转换为字符串并从中删除时间时,它正确地将该变量设置为 24/08/2018。 But when I then go to compare them in the Import cmdlet, it just returns all data in the CSV, rather than dates less than 24/08.但是当我在导入 cmdlet 中比较它们时,它只返回 CSV 中的所有数据,而不是小于 24/08 的日期。

I also know Powershell can compare these, because if I create a separate variable $otherDate with 24/08/2018 in it, and then create an if statement that checks if $weekFinal is greater or less than $otherDate, it correctly runs the statement when true.我也知道 Powershell 可以比较这些,因为如果我创建一个单独的变量 $otherDate,其中包含 24/08/2018,然后创建一个 if 语句来检查 $weekFinal 是大于还是小于 $otherDate,它会正确运行该语句当真。

I've tried using both Where and Where-Object in the Import-Csv cmdlet, but both have returned the same results.我尝试在 Import-Csv cmdlet 中同时使用 Where 和 Where-Object,但都返回了相同的结果。

How do I get Powershell to correctly compare $_.collectionDate and $weekFinal to filter the imported data from the csv?如何让 Powershell 正确比较 $_.collectionDate 和 $weekFinal 以过滤从 csv 导入的数据?

It is easiest to perform all calculations and comparisons using [datetime] instances - avoid string operations (except for converting a string representation of a date to a [datetime] instance).使用[datetime]实例执行所有计算和比较最简单的 - 避免字符串操作(将日期的字符串表示形式转换为[datetime]实例除外)。

First, express today's date and 1 week from now as a [datetime] instance without a time-of-day component:首先,将今天的日期和从现在起 1 周的日期表示为没有时间组件的[datetime]实例:

$today = (Get-Date).Date  # Today's date, without a time component (midnight)
$oneWeekFromToday = $today.AddDays(7)

Then use [datetime]::Parse() to convert the invariably string-typed CSV column value of interest to a [datetime] instance so you can perform proper date-comparison.然后使用[datetime]::Parse()将感兴趣的总是字符串类型的 CSV 列值转换为[datetime]实例,以便您可以执行适当的日期比较。

$import = Import-Csv \\location\EmailCSV.csv | Where-Object { 
  $thisDate = [datetime]::Parse($_.collectionDate)
  # Process this row if its date falls between today and 1 week from now, inclusively.
  $thisDate -ge $today -and $thisDate -le $oneWeekFromToday
}

Important : use [datetime]::Parse('...') , not a [datetime] '...' cast , because only [datetime]::Parse() respects the current culture 's date and time formats;重要提示使用[datetime]::Parse('...') ,而不是[datetime] '...' cast ,因为只有[datetime]::Parse()尊重当前文化的日期和时间格式; by design, PowerShell's casts and string interpolation always use the invariant culture , irrespective of the current culture - for more information, see this answer of mine.按照设计,PowerShell 的强制转换和字符串插值始终使用不变文化,而不管当前的文化如何 - 有关更多信息,请参阅我的这个答案

Try casting the strings to [datetime] in your comparison.尝试在比较中将字符串转换为[datetime] When using a sample set of data, I was able to get the expected results.当使用一组样本数据时,我能够得到预期的结果。

$import = Import-Csv @("\\location\EmailCSV.csv") | Where-Object {[datetime]$_.collectionDate -lt [datetime]$weekFinal}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM