[英]Powershell list the dates between a range of dates.
I am writing a script in PowerShell
and I need to know the dates between a range of dates.我正在PowerShell
编写脚本,我需要知道一系列日期之间的日期。
The script is to automate reporting.该脚本用于自动报告。 Let's say there is a bunch of logged data, but the data only gets collected on weekdays, no weekends and no holidays.假设有一堆记录的数据,但数据只在工作日收集,没有周末也没有假期。 I FTP and collect the last 15 generations logged.我 FTP 并收集记录的最后 15 代。
With the script I open up the latest instance of the report in Excel
.使用脚本,我在Excel
打开了报告的最新实例。 Once the workbook has opened up I read a cell which contains a date, that date will determine when the last time the report has been ran on.打开工作簿后,我读取了一个包含日期的单元格,该日期将确定上次运行报告的时间。 (Today's data however will not be accessible until tomorrow, so it is always 1 day behind.) (然而,今天的数据要到明天才能访问,所以它总是落后 1 天。)
When I than have that date, I use Get-Date
to get the current date.当我有那个日期时,我使用Get-Date
来获取当前日期。 I need to know the dates between the current date and the date I read in from Excel.我需要知道当前日期和我从 Excel 读入的日期之间的日期。 Afterwards I will need to see if any of the dates have fallen on a holiday, or if they have fell on the weekend.之后,我将需要查看是否有任何日期落在假期,或者是否落在周末。
$excelDate = $excel.Cells.Item(1489, 1).Value()
$currentDate = Get-Date
Let's say $excelDate = "03/07/2014"
假设$excelDate = "03/07/2014"
and $currentDate = "03/14/2014"
和$currentDate = "03/14/2014"
What I would like to get is 03/08/2014, 03/09/2014, 03/10/2014, 03/11/2014, 03/12/2014, 03/13/2014.我想得到的是 03/08/2014、03/09/2014、03/10/2014、03/11/2014、03/12/2014、03/13/2014。
Once I have those dates I can check to see if any of them were a holiday, and if they have fallen on a weekend.一旦我有了这些日期,我就可以检查其中是否有假期,以及它们是否在周末。
If they fell on a holiday I can subtract 1 day (or 2 days if the last day in the report fell on a Friday...I am still trying to figure out how I am going to do this.), and use a switch
to get-content
of the proper data before I begin to parse through and put it into Excel
.如果他们遇上假期,我可以减去 1 天(如果报告中的最后一天是星期五,则减去 2 天......我仍在努力弄清楚我将如何做到这一点。),并使用switch
在我开始解析并将其放入Excel
之前get-content
正确数据get-content
。
So something like this:所以像这样:
switch($daysMissed)
{
1 {Write-Host "The Report is currently up to date!!" }
2 {$data = get-content C:\Users\$userName\Desktop\Report\data\data.txt }
3 {$data = get-content C:\Users\$userName\Desktop\Report\data\data2.txt }
4 {$data = get-content C:\Users\$userName\Desktop\Report\data\data3.txt }
5 {$data = get-content C:\Users\$userName\Desktop\Report\data\data4.txt }
6 {$data = get-content C:\Users\$userName\Desktop\Report\data\data5.txt }
7 {$data = get-content C:\Users\$userName\Desktop\Report\data\data6.txt }
8 {$data = get-content C:\Users\$userName\Desktop\Report\data\data7.txt }
9 {$data = get-content C:\Users\$userName\Desktop\Report\data\data8.txt }
10 {$data = get-content C:\Users\$userName\Desktop\Report\data\data9.txt }
11 {$data = get-content C:\Users\$userName\Desktop\Report\data\data10.txt }
}
Once I have the proper data, I do a bunch of stuff within Excel
and finish off that days report, then I would go $daysMissed = $daysMissed - 1
.一旦我有了正确的数据,我就会在Excel
做一堆事情并完成那一天的报告,然后我会去$daysMissed = $daysMissed - 1
。
So my question is how exactly would I list the dates, between a range of dates?所以我的问题是我将如何在一系列日期之间列出日期?
or, is there an easier way to do what I am doing?或者,有没有更简单的方法来做我正在做的事情?
Thanks for the help!谢谢您的帮助!
Maybe something like:也许是这样的:
$excelDate = get-date "03/07/2014"
$currentDate = get-date "03/14/2014"
for ( $i = $excelDate.AddDays(1); $i -lt $currentDate; $i=$i.AddDays(1) ) {
$i.ToShortDateString()
}
Something like this, maybe?像这样的东西,也许?
$excelDate = "03/07/2014"
$currentDate = "03/14/2014"
$start = [Datetime]$excelDate
$end = [Datetime]$currentDate
While ($start -lt $end)
{
$start = $start.AddDays(1)
$start.ToShortDateString()
}
3/8/2014
3/9/2014
3/10/2014
3/11/2014
3/12/2014
3/13/2014
3/14/2014
this?这个?
PS C:\ps> [datetime]$a = "03/07/2014"
PS C:\ps> [datetime]$b = "03/14/2014"
PS C:\ps> 1..(($b -$a).days-1) | % {$a.AddDays($_)}
sabato 8 marzo 2014 00:00:00
domenica 9 marzo 2014 00:00:00
lunedì 10 marzo 2014 00:00:00
martedì 11 marzo 2014 00:00:00
mercoledì 12 marzo 2014 00:00:00
giovedì 13 marzo 2014 00:00:00
or或者
PS C:\ps> 1..(($b -$a).days-1) | % {$a.AddDays($_).ToShortDateString()}
08/03/2014
09/03/2014
10/03/2014
11/03/2014
12/03/2014
13/03/2014
Here is another example that writes to a file.这是另一个写入文件的示例。 You should adjust the $path variable to something that fits your need.您应该将 $path 变量调整为适合您需要的值。
function GenerateDates($beginDate,$EndDate)
{
$path="c:\temp\data.csv"
"day,week" | Out-File -Append -LiteralPath $path
$date=$beginDate
$week=$beginDate
Do{
for($i=1;$i -lt 7;$i++)
{
$d1=$date.ToString("yyyy-MM-dd")
$d2=$week.ToString("yyyy-MM-dd")
"$d1,$d2" | Out-File -Append -LiteralPath $path
$date=$date.AddDays(1)
}
$date=$date.AddDays(1)
$week=$date
}Until($date -gt $endDate)
}
$beginDate=[datetime]::Parse("2017-09-10")
$endDate=[datetime]::Parse("2018-06-10")
GenerateDates $beginDate $endDate
I noticed that few of these snippets actually included the start date in the output.我注意到这些片段中实际上很少在输出中包含开始日期。 Just remember to call .AddDays() after the output, and to change the -lt
to -le
(less than to less than equal to).请记住在输出后调用 .AddDays(),并将-lt
更改为-le
(小于到小于等于)。
I do understand that was not the initial question posed, but I had a similar question as OP, hopefully this will assist anyone else with a similar problem (printing all dates within a given range).我确实明白这不是最初提出的问题,但我有一个与 OP 类似的问题,希望这能帮助其他有类似问题的人(打印给定范围内的所有日期)。
Function Build-Calendar() {
Param (
[System.DateTime] $PeriodStart,
[System.DateTime] $PeriodEnd
)
while ($PeriodStart -le $PeriodEnd) {
$PeriodStart.ToShortDateString()
$PeriodStart = $PeriodStart.AddDays(1)
}
}
$testParameters = @{
PeriodStart = "09/16/2020"
PeriodEnd = "09/30/2020"
}
Build-Calendar @testParameters
9/16/2020
9/17/2020
9/18/2020
9/19/2020
9/20/2020
9/21/2020
9/22/2020
9/23/2020
9/24/2020
9/25/2020
9/26/2020
9/27/2020
9/28/2020
9/29/2020
9/30/2020
For all those who like something terse and one-line-y, here's a quick/compact solution:对于所有喜欢简洁和单行的人,这里有一个快速/紧凑的解决方案:
$calendar = ({ param([datetime]$from, [datetime]$to, [int]$i=0) do { $from=$from.AddDays($i++);$from } while ($from -le $to) }).Invoke('2021-01-01', (Get-Date))
...and for anyone who wants this broken down a little, here's the same code on multiple lines: ...对于任何想要稍微分解一下的人,这里有多行相同的代码:
$calendar = {
param(
[datetime]$from
, [datetime]$to
, [int] $i=0
)
do {
$from = $from.AddDays($i++)
$from
} while ($from -le $to)
}.Invoke('2021-01-01', (Get-Date))
...and finally with a few comments ...最后还有一些评论
$calendar = { # assign the results of the rest of the code to $calendar
# & begin an anonymous script block, which we can invoke
param( # begin param declaration
[datetime]$from # declare the from datetime param
, [datetime]$to # declare the to datetime param
, [int] $i=0 # declare the counter/offset for our do-loop, which we
# have a default as 0, which is a shortcut way of declaring
# the variable instead of inside the script block
) # end param declaration
do { # begin the do-while loop
$from = $from.AddDays($i++) # increment the start date by one day
$from # return this date to the receiving variable
} while ($from -le $to) # repeat the do-while if from <= to
}.Invoke('2021-01-01', (Get-Date)) # finally, we invoke the anonymous function and pass in
# our start and end dates
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.