Calculate x previous working days (exclude weekends)

Warzou 101 Reputation points
2022-05-30T11:52:04.443+00:00

Hi,

I am looking for a way to get the date of the previous business day (without weekends - count only weekdays).

For example :

30/05/2022 -10 days = 16/05/2022.

I found this script :

$startdate = (Get-Date).AddDays(-10)  
$enddate = Get-Date  
  
$difference = New-TimeSpan -Start $startdate -End $enddate  
$difference.Days  
  
$days = [Math]::Ceiling($difference.TotalDays)+1  
  
1..$days | ForEach-Object {  
  $startdate  
  $startdate = $startdate.AddDays(1)  
} |  
  Where-Object { $_.DayOfWeek -gt 0 -and $_.DayOfWeek -lt 6}  

The result :

206861-2022-05-30-22-36-48-windows-powershell-ise.png

Could someone help me?

Thank you

Windows for business | Windows Server | User experience | PowerShell
{count} votes

Accepted answer
  1. MotoX80 36,401 Reputation points
    2022-05-31T02:59:13.043+00:00

    I'm confused. I don't see how "$_.DayOfWeek -gt 0" is working because on my pc (Win11, PS5.1) that is an alpha value.

    DateTime    : Wednesday, May 25, 2022 12:00:00 AM
    Date        : 5/25/2022 12:00:00 AM
    Day         : 25
    DayOfWeek   : Wednesday
    DayOfYear   : 145
    Hour        : 0
    Kind        : Unspecified
    Millisecond : 0
    Minute      : 0
    Month       : 5
    Second      : 0
    Ticks       : 637890336000000000
    TimeOfDay   : 00:00:00
    Year        : 2022
    

    As to the question; 10 business days is the known value and the problem is to solve for the calendar date. Correct?

    Here is what I came up with.

    $days = 10                           # number of past business days to calculate
    $weekend = 'Saturday','Sunday'       
    $startdate = Get-Date
    $busdays = @()                          # array of business days  
    while($true) {
        $startdate = $startdate.AddDays(-1)
        if ($weekend -notcontains $startdate.DayOfWeek) {  
            $busdays += $startdate
        }     
        if ($busdays.count -ge $days) {
             break
        }
    }
    cls
    "Here are the prior $days business days."
    $busdays
    ""
    "Here is the date you are looking for."
    $busdays[$days - 1]        # because the array in relative to zero 
    

1 additional answer

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2022-05-31T01:52:22.887+00:00

    Try this:

    $startdate = (Get-Date "5/16/2022").Date        # midnight -- START of day on Monday
    $enddate =   $startdate.Date.AddDays(10)        # midnight -- START of workday on the following Thursday (i.e., the END of Wednesday)
    
    $difference = New-TimeSpan -Start $startdate -End $enddate  # should be 10 days (Monday .. Wednesday)
    $days = $difference.Days
    
    1..$days | ForEach-Object {
        $startdate
        $startdate = $startdate.AddDays(1)
    
    } |
        Where-Object { $_.DayOfWeek -gt 0 -and $_.DayOfWeek -lt 6 }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.