如何获得一年中给定工作日的第5天(周一、周二、周三、周四、周五、周六、周日)的列表



我需要能够在Outlook中为任何给定月份的第5个星期五创建约会/会议;但Outlook没有提供这样做的方法,因为一年中只有3到4个月的时间是一周中任何一天的第五天。

手动创建会议请求不是问题,但除了每年翻阅日历之外,是否有一种程序化的方法至少可以找到适用的日期?最好,在PowerShell中有一个解决方案会有所帮助,因为PowerShell几乎在每台Windows计算机上都有。

是的,下面的函数将查找给定月份中一周中某一特定日期的5周内的所有事件。

Function getAllFifthWeekDay(){
[CmdletBinding()]
param (
[Parameter(Position=0)][DayOfWeek]$DayOfWeek='Friday',
[Parameter(Position=1)][int]$Year=(Get-Date).Year
)
[DateTime[]]$output=@()
$date = Get-Date "01/01/$($Year)"
#Get First Day of Year on day
$date = $date.AddDays($(if([int]$DayOfWeek -gt [int]$date.DayOfWeek){
$DayOfWeek-[int]$date.DayOfWeek    
}else{
$DayOfWeek-[int]$date.DayOfWeek+7
})) 
$count = 0;
$lastMonth = $date.Month;
do{
$lastMonth = $date.Month;
$date = $date.AddDays(7)
if($lastMonth -ne $date.Month){
$count = 0
}
if($count -gt 3){
$output += $date
}
$count += 1;
} while ($date.Year -eq $Year)
return $output
}

要运行此:

  1. 在Windows中打开";Windows Powershell";从「开始」菜单
  2. 将上面的代码粘贴到命令提示符中;输入";以注册该函数
  3. 执行如下函数,替换您要查找的工作日和年份:

getAllFifthWeekDay 'Friday' '2022'

您应该看到如下输出:

Friday, April 29, 2022 12:00:00 AM
Friday, July 29, 2022 12:00:00 AM
Friday, September 30, 2022 12:00:00 AM
Friday, December 30, 2022 12:00:00 AM

-----------------------当天晚些时候编辑------------------------

实际上,我创建了一个完整的工作示例,它不仅会找到一个月中某一天的第5天,而且会为该天创建一个会议邀请,因此这是一个完全自动化的过程。希望这能帮助其他人。

您使用以下内容执行代码:

Create-AppointmentFifthWeek -Subject 'Lunch - Strategic Planning' -StartTime "12:30:00" -DurationInMinutes 120 -Categories "Strategy"

以下是实现这一点的完整代码:

Function Create-AppointmentFifthWeek(){
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)][String]$Subject,
[Parameter(Mandatory=$true)][String]$StartTime,
[Parameter()][String]$Recipients,
[Parameter()][String]$Categories,
[Parameter()][String]$Location,
[Parameter()][String]$Body=$Subject,
[Parameter()][int]$ReminderMinutesBeforeStart=15,
[Parameter()][int]$DurationInMinutes=30,
[Parameter()][DayOfWeek]$DayOfWeek='Friday',
[Parameter()][int]$Year=(Get-Date).Year
)
foreach($date in $(getAllFifthWeekDay -DayOfWeek $DayOfWeek -Year $Year)){
$StartDateTime = $date.Add([System.Timespan]::Parse($StartTime)) 
Create-Appointment -Subject $Subject -Recipients $Recipients -Categories $Categories -Location $Location -Body $Body -ReminderMinutesBeforeStart $ReminderMinutesBeforeStart -DurationInMinutes $DurationInMinutes -MeetingStart $StartDateTime
}
}

Function getAllFifthWeekDay(){
[CmdletBinding()]
param (
[Parameter(Position=0)][DayOfWeek]$DayOfWeek='Friday',
[Parameter(Position=1)][int]$Year=(Get-Date).Year
)
[DateTime[]]$output=@()
$date = Get-Date "01/01/$($Year)"
#Get First Day of Year on day
$date = $date.AddDays($(if([int]$DayOfWeek -gt [int]$date.DayOfWeek){
$DayOfWeek-[int]$date.DayOfWeek    
}else{
$DayOfWeek-[int]$date.DayOfWeek+7
})) 
$count = 0;
$lastMonth = $date.Month;
do{
$lastMonth = $date.Month;
$date = $date.AddDays(7)
if($lastMonth -ne $date.Month){
$count = 0
}
if($count -gt 3){
$output += $date
}
$count += 1;
} while ($date.Year -eq $Year)
return $output
}

Function Create-Appointment(){
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)][String]$Subject,
[Parameter(Mandatory=$true)][DateTime]$MeetingStart,
[Parameter()][String]$Recipients,
[Parameter()][String]$Categories,
[Parameter()][String]$Location,
[Parameter()][String]$Body=$Subject,
[Parameter()][int]$ReminderMinutesBeforeStart=15,
[Parameter()][int]$DurationInMinutes=30
)
$ol = New-Object -ComObject Outlook.Application
$meeting = $ol.CreateItem('olAppointmentItem')
if($ReminderMinutesBeforeStart -gt 0){
$meeting.ReminderSet = $true
$meeting.ReminderMinutesBeforeStart = $ReminderMinutesBeforeStart
}
if($PSBoundParameters.ContainsKey('Recipients')){
foreach($email in $Recipients -split ";"){
if($email -ne ''){
$meeting.Recipients.Add($email)
}
}
$meeting.MeetingStatus = [Microsoft.Office.Interop.Outlook.OlMeetingStatus]::olMeeting
} else {
$meeting.MeetingStatus = [Microsoft.Office.Interop.Outlook.OlMeetingStatus]::olNonMeeting
}
if($PSBoundParameters.ContainsKey('Location')){
$meeting.Location = $Location
}
if($PSBoundParameters.ContainsKey('Categories')){
$meeting.Categories = $Categories
} 
$meeting.Subject = $Subject
$meeting.Body = $Body
$meeting.Start = $MeetingStart
$meeting.Duration = $DurationInMinutes
$meeting.Save()    
$meeting.Send()    
}

几个循环加上几个检查,你就得到了一个月第5周的星期五列表

foreach ($year in 2022..2024) {
foreach ($month in 1..12) {
if (($maxDays = [datetime]::DaysInMonth($year, $month)) -gt 28) {
foreach ($day in 29..$maxDays) {
if (($date = [datetime]"$year-$month-$day").DayOfWeek -eq [System.DayOfWeek]::Friday) {
$date.ToString("D")
}
}
}
}
}
Friday, April 29, 2022
Friday, July 29, 2022
Friday, September 30, 2022
Friday, December 30, 2022
Friday, March 31, 2023
Friday, June 30, 2023
Friday, September 29, 2023
Friday, December 29, 2023
Friday, March 29, 2024
Friday, May 31, 2024
Friday, August 30, 2024
Friday, November 29, 2024

相关内容

  • 没有找到相关文章

最新更新