How to get the next business day in powershell

前端 未结 1 1923
温柔的废话
温柔的废话 2021-01-21 10:37

I have the following test code. Basically I am checking when a new file is created in folder. I need to know that if the file was created after 4pm display the next business da

1条回答
  •  爱一瞬间的悲伤
    2021-01-21 11:00

    Adding to what jisaak said: "business day" is organization specific. Some organizations don't work on holidays that other organizations do. If you want to handle holidays correctly you'll need an explicit list of holidays for your business

    Omitting the formatting details (which OP seems to understand) this should do it:

    #  $date is input date
    $nextBizDay = $date.affffdays(1)
    
    # You would probably want to generate the follow list programmatically,
    #  instead of manually as done here
    $holidays = 1,   <# New Years #>
                18,  <# MLK day 2016 #>
                     <# other holidays encoded as Day Of Year #>
                360  <# Christmas in a Leap Year #>
    # An alternative to a list of holidays like this is to find a web service 
    #  you can query to get the holidays for a given year
    
    while ($nextBizDay.DayOfWeek -eq 'Saturday' -or
           $nextBizDay.DayOfWeek -eq 'Sunday' -or
           $nextBizDay.DayOfYear -in $holidays) {
      if ($nextBizDay.DayOfYear -gt 366) {
        throw "No next business day this year. Need to add additional logic"
      }
      $nextBizDay = $nextBizDay.affffdays(1)
    }
    

    0 讨论(0)
提交回复
热议问题