如何在powershell中获得下一个工作日 [英] How to get the next business day in powershell

查看:80
本文介绍了如何在powershell中获得下一个工作日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下测试代码.基本上我正在检查何时在文件夹中创建新文件.我需要知道,如果文件是在下午 4 点之后创建的,则显示下一个工作日.目前我的代码显示在第二天,但我需要显示下一个工作日.任何帮助,将不胜感激.

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 day. Currently my code displays the next day, but I need to display the next business day. Any help would be appreciated.

$formatteddate = "{0:h:mm:ss tt}" -f (get-date)

if ($formatteddate -gt "4:00:00 PM"){
$(Get-Date).AddDays(1).ToString('MMM d yyyy')
}

推荐答案

补充一下 jisaak 所说的:工作日"是特定于组织的.有些组织不像其他组织那样在假期工作.如果您想正确处理假期,您需要为您的企业制定明确的假期列表

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

省略格式细节(OP似乎理解)这应该这样做:

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

#  $date is input date
$nextBizDay = $date.adddays(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.adddays(1)
}

这篇关于如何在powershell中获得下一个工作日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆