gpt4 book ai didi

vb.net - 如何获得给定月份的第一个、第二个或最后一个星期二(或一周中的任何一天)

转载 作者:行者123 更新时间:2023-12-04 04:50:20 26 4
gpt4 key购买 nike

我在 VB.net 中寻找一个函数,它返回一个月中给定周的指定工作日的日期。类似于 Outlook 约会的内容。

例如:
每个月的第一个星期一,
每月第二个星期四或
本月最后一个星期六

我进行了搜索,但找不到适合所有选项的通用工具。所以我创建了自己的并想分享它。

最佳答案

这是我想出的:

Public Function GetDate()

Dim dt As Date = Today

Dim FirstWeek As Integer = 1
Dim SecondWeek As Integer = 2
Dim ThirdWeek As Integer = 3
Dim FourthWeek As Integer = 4
Dim LastWeek As Integer = 5

MsgBox(GetNthDayOfNthWeek(dt, DayOfWeek.Monday, LastWeek).ToString)

End Function

Public Function GetNthDayOfNthWeek(ByVal dt As Date, ByVal DayofWeek As Integer, ByVal WhichWeek As Integer) As Date
'specify which day of which week of a month and this function will get the date
'this function uses the month and year of the date provided

'get first day of the given date
Dim dtFirst As Date = DateSerial(dt.Year, dt.Month, 1)

'get first DayOfWeek of the month
Dim dtRet As Date = dtFirst.AddDays(6 - dtFirst.AddDays(-(DayofWeek + 1)).DayOfWeek)

'get which week
dtRet = dtRet.AddDays((WhichWeek - 1) * 7)

'if day is past end of month then adjust backwards a week
If dtRet >= dtFirst.AddMonths(1) Then
dtRet = dtRet.AddDays(-7)
End If

'return
Return dtRet

End Function

享受!

关于vb.net - 如何获得给定月份的第一个、第二个或最后一个星期二(或一周中的任何一天),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17520008/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com