如何在 Powershell 中计算 X 个工作日?
How to count X working days back in powershell?
任何人都可以帮助我使用 powershell 脚本,该脚本可以从当前日期倒退 X 个工作日吗?该脚本应询问要倒退多少个工作日,输出将是某种倒退日期。
示例:我想知道我需要倒退多少个日历日才能获得 20 个工作日。所以我将输入数字 20,输出将是 12/31/2014。因此,为了获得 20 个工作日,我需要从 2014 年 12 月 31 日开始计算(今天和 12 月 31 日之间有一天假期 (01/01/2015)。)
工作日为周一至周五,美国法定节假日除外。
我发现了很多类似的代码来计算两天之间的天数,比如这个 http://powershell.com/cs/blogs/tips/archive/2014/07/07/finding-dates-between-two-dates.aspx
或者这个
http://www.integrationtrench.com/2014/05/counting-days-with-powershell.html
但没有代码完全符合我的需要。
感谢您的帮助。
威乐
这是我要修改的代码...
$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 我发誓我没有从您的链接 post 窃取条件逻辑。我独立思考的:)
示例输出 今天在这种情况下是 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
任何人都可以帮助我使用 powershell 脚本,该脚本可以从当前日期倒退 X 个工作日吗?该脚本应询问要倒退多少个工作日,输出将是某种倒退日期。
示例:我想知道我需要倒退多少个日历日才能获得 20 个工作日。所以我将输入数字 20,输出将是 12/31/2014。因此,为了获得 20 个工作日,我需要从 2014 年 12 月 31 日开始计算(今天和 12 月 31 日之间有一天假期 (01/01/2015)。) 工作日为周一至周五,美国法定节假日除外。
我发现了很多类似的代码来计算两天之间的天数,比如这个 http://powershell.com/cs/blogs/tips/archive/2014/07/07/finding-dates-between-two-dates.aspx
或者这个 http://www.integrationtrench.com/2014/05/counting-days-with-powershell.html
但没有代码完全符合我的需要。
感谢您的帮助。
威乐
这是我要修改的代码...
$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 我发誓我没有从您的链接 post 窃取条件逻辑。我独立思考的:)
示例输出 今天在这种情况下是 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