我正在尝试创建一个 powershell 脚本,该脚本可以从当前日期算起 X 个工作日?该脚本应该询问需要回溯多少个工作日,并且输出将是某种回溯的日期。
示例:我想知道需要回溯多少个日历日才能得到 20 个工作日。所以我输入数字 20,输出将为 12/31/2014。因此,为了获得20个工作日,我需要从2014年12月31日开始计算(今天到12月31日之间有一天假期(01/01/2015)。)
工作日为周一至周五,美国法定节假日除外。
我发现了很多类似的解决方案来计算两天之间的天数:
但没有一个能完全满足我的需要。
这是我正在尝试修改的代码...
$startdate = (Get-Date).date
$enddate = Get-Date -Date '2014-12-20'
$New_Years_Day2014 = Get-Date -Date '2014-01-01'
$Martin_Luther_King2014 = Get-Date -Date '2014-01-20'
$Washingtons_Birthday2014 = Get-Date -Date '2014-02-17'
$Good_Friday2014 = Get-Date -Date '2014-04-18'
$Memorial_Day2014 = Get-Date -Date '2014-05-26'
$Independence_Day2014 = Get-Date -Date '2014-07-04'
$Labor_Day2014 = Get-Date -Date '2014-09-01'
$Thanksgiving_Day2014 = Get-Date -Date '2014-11-27'
$Christmas2014 = Get-Date -Date '2014-12-25'
$New_Years_Day2015 = Get-Date -Date '2015-01-01'
$Martin_Luther_King2015 = Get-Date -Date '2015-01-19'
$Washingtons_Birthday2015 = Get-Date -Date '2015-02-16'
$Good_Friday2015 = Get-Date -Date '2015-04-03'
$Memorial_Day2015 = Get-Date -Date '2015-05-25'
$Independence_Day2015 = Get-Date -Date '2015-07-03'
$Labor_Day2015 = Get-Date -Date '2015-09-07'
$Thanksgiving_Day2015 = Get-Date -Date '2015-11-26'
$Christmas2015 = Get-Date -Date '2015-12-25'
$difference = New-TimeSpan -Start $startdate -End $enddate
"Days in all: " + $difference.Days
$days = [Math]::Ceiling($difference.TotalDays)+1
$workdays = 0..$days | ForEach-Object {
$startdate
$startdate = $startdate.AddDays(1)
} |
Where-Object { $_.DayOfWeek -gt 0 -and $_.DayOfWeek -lt 6 -and $startdate -ne $New_Years_Day2014 -and $startdate -ne $Martin_Luther_King2014 -and $startdate -ne $Washingtons_Birthday2014 -and $startdate -ne $Good_Friday2014 -and $startdate -ne $Memorial_Day2014 -and $startdate -ne $Independence_Day2014 -and $startdate -ne $Labor_Day2014 -and $startdate -ne $Thanksgiving_Day2014 -and $startdate -ne $Christmas2014 -and $startdate -ne $New_Years_Day2015 -and $startdate -ne $Martin_Luther_King2015 -and $startdate -ne $Washingtons_Birthday2015 -and $startdate -ne $Good_Friday2015 -and $startdate -ne $Memorial_Day2015 -and $startdate -ne $Independence_Day2015 -and $startdate -ne $Labor_Day2015 -and $startdate -ne $Thanksgiving_Day2015 -and $startdate -ne $Christmas2015} |
Measure-Object |
Select-Object -ExpandProperty Count
"Workdays: $workdays"
此函数将采用两个值。第一个是您正在寻找日期匹配的工作日数。第二个是可选的,允许您指定开始日期。如果省略,则使用今天的日期。
Function Get-WorkingDay{
param(
[Parameter(Mandatory=$True,Position=1)]
[int]$maxWorkingDays,
[Parameter(Mandatory=$False,Position=2)]
[datetime]$startDate = (Get-Date).Date
)
$holidays = @(
(Get-Date -Date '2014-01-01'), # New_Years_Day2014
(Get-Date -Date '2014-01-20'), # Martin_Luther_King2014
(Get-Date -Date '2014-02-17'), # Washingtons_Birthday2014
(Get-Date -Date '2014-04-18'), # Good_Friday2014
(Get-Date -Date '2014-05-26'), # Memorial_Day2014
(Get-Date -Date '2014-07-04'), # Independence_Day2014
(Get-Date -Date '2014-09-01'), # Labor_Day2014
(Get-Date -Date '2014-11-27'), # Thanksgiving_Day2014
(Get-Date -Date '2014-12-25'), # Christmas2014
(Get-Date -Date '2015-01-01'), # New_Years_Day2015
(Get-Date -Date '2015-01-19'), # Martin_Luther_King2015
(Get-Date -Date '2015-02-16'), # Washingtons_Birthday2015
(Get-Date -Date '2015-04-03'), # Good_Friday2015
(Get-Date -Date '2015-05-25'), # Memorial_Day2015
(Get-Date -Date '2015-07-03'), # Independence_Day2015
(Get-Date -Date '2015-09-07'), # Labor_Day2015
(Get-Date -Date '2015-11-26'), # Thanksgiving_Day2015
(Get-Date -Date '2015-12-25') # Christmas2015
)
# Count down the working days checking each date as we progress
$dateIndex = $startDate
For($workingDayIndex = $maxWorkingDays; $workingDayIndex -gt 0; $workingDayIndex--){
# Assume the current day is not a working day
$isWorkingDay = $False
Do{
If (("Sunday","Saturday" -contains $dateIndex.DayOfWeek) -or ($holidays -contains $dateIndex)){
# This is not a working day. Check the next day.
# Write-Host "$($dateIndex.Date) is a weekend or holiday" -ForegroundColor Red
$dateIndex = $dateIndex.AddDays(-1)
} Else {
# Current $dateIndex is a working day.
$isWorkingDay = $True
}
} While(!$isWorkingDay)
# Write-Host "Current Date: $($dateIndex.Date). Number of working days left: $workingDayIndex."
# Set the $dateIndex to the previous day.
$dateIndex = $dateIndex.AddDays(-1)
}
# The last date was the correct one. Re-add the day.
$dateIndex.AddDays(1)
}
Get-WorkingDay 10
我们将您的假期日期转换为数组,这将使以后的比较变得更加容易。这是一项手动练习,因此您需要确保定期更新日期。
使用变量
$maxWorkingDays
来确定我们需要回溯多少个工作日,并使用 $workingDayIndex
来跟踪进度。我们使用 For
循环来倒计时工作日,直到达到 1。在循环中,我们检查前一天是否是工作日。如果不是更小的 Do..While
循环会将日期向后设置,直到到达工作日。我放置了一些注释掉的 Write-Host
行,可以帮助解释您在循环位置中的位置。
-contains
为我们完成了这项工作,因为我们在比较中使用数组,并检查元素之一是否在上述数组中。 @AnsgarWiechers 我发誓我没有从您链接的帖子中窃取条件逻辑。我独立思考的:)
示例输出 本例中的今天是 2015 年 1 月 31 日星期六 12:00:00 AM
PS C:\Users\Cameron> Get-WorkingDay 5
Monday, January 26, 2015 12:00:00 AM
PS C:\Users\Cameron> Get-WorkingDay 5 "02/01/2015"
Monday, January 26, 2015 12:00:00 AM
PS C:\Users\Cameron> Get-WorkingDay 5 "2014-11-27"
Thursday, November 20, 2014 12:00:00 AM