• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 59
  • Last Modified:

Powershell : Compute working days without we and holidays

Powershell  : script to compute working days (for ex +5 days) from date xxxx with excluded holidays days (based on list).

anybody has this available ?

Tks !
0
François VDC
Asked:
François VDC
1 Solution
 
NorieVBA ExpertCommented:
Do you want to get the date N working days from date XXXX?
0
 
aikimarkCommented:
something like this, but in function form?
$a = [DATETIME]'2/2/2018'
$workdays=5
$offset=0
$foundworkdays=0
while($foundworkdays -lt $workdays) { 
    $offset++
    if (('Saturday', 'Sunday') -notcontains $a.AddDays($offset).dayofweek) {$foundworkdays++}
    }
$a.AddDays($offset)

Open in new window

1
 
François VDCIcT service DeskAuthor Commented:
anybody ?
0
 
aikimarkCommented:
anybody ?
Did you read my comment?
0
 
Sajen JoseCommented:
I am not sure if this is what you are looking for:

Clear-Host

#Holiday List - Keep adding Holidays and then add thos variables into the $Holidays Array
[DateTime]$HOL01 = "01/02/2018"
[DateTime]$HOL02 = "02/01/2018"

$Holidays = @($HOL01, $HOL02)

Function WorkingDays
{
    $Weekends = [System.DayOfWeek]::Saturday, [System.DayOfWeek]::Sunday #Weekends
    
    #Change the day, month and Year based on your values, the start date from which you want to calculate
    $StartDate = Get-Date -Year 2018 -Month 1 -Day 1 
    
    $ToDate = (Get-Date).AddDays(-1)


    $WorkDays = 0

    while ($StartDate -le $ToDate) {
        $StartDate = $StartDate.AddDays(1)

        If(($StartDate.DayOfWeek -notin $Weekends) -and ($StartDate.Date -notin $Holidays))
        {
            $WorkDays++
        }
    }

    $WorkDays
}

#Call the function to execute
WorkingDays

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

On-Demand: Securing Your Wi-Fi for Summer Travel

Traveling this summer?Check out our on-demand webinar to learn about the importance of Wi-Fi security and 3 easy measures you can start taking immediately to protect your private data while using public Wi-Fi. Follow us today to learn more!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now