0

想知道是否有一个内置于 VB.net 的函数可以告诉我它属于哪个周数。

IE。2013-05-10 =“第 2 周的一部分” 2013-05-31 =“第 5 周的一部分”

datetimepicker 是否带有此功能?提前致谢!

4

1 回答 1

2

您可以将所选月份第一天的 WeekOfYear 与所选日期的 WeekOfYear 进行比较。

Dim dtDate1 As DateTime = New DateTime(2013, 5, 10)
MsgBox(DatePart(DateInterval.WeekOfYear, dtDate1, FirstDayOfWeek.System) - DatePart(DateInterval.WeekOfYear, New DateTime(dtDate1.Year, dtDate1.Month, 1), FirstDayOfWeek.System) + 1)

Dim dtDate2 As DateTime = New DateTime(2013, 5, 31)
MsgBox(DatePart(DateInterval.WeekOfYear, dtDate2, FirstDayOfWeek.System) - DatePart(DateInterval.WeekOfYear, New DateTime(dtDate2.Year, dtDate2.Month, 1), FirstDayOfWeek.System) + 1)

或者作为一个函数:

Function GetWeekInMonth(dtDate As Date) As Integer
    Dim DateWeekNumber As Integer = DatePart(DateInterval.WeekOfYear, dtDate, FirstDayOfWeek.System)
    Dim MonthStartWeek As Integer = DatePart(DateInterval.WeekOfYear, New DateTime(dtDate.Year, dtDate.Month, 1), FirstDayOfWeek.System)
    Return DateWeekNumber - MonthStartWeek + 1
End Function  

但这作为日期对象的扩展添加真的很棒:

Module Extensions
    <System.Runtime.CompilerServices.Extension> _
    Public Function GetWeekInMonth(dtDate As Date) As Integer
        Dim DateWeekNumber As Integer = DatePart(DateInterval.WeekOfYear, dtDate, FirstDayOfWeek.System)
        Dim MonthStartWeek As Integer = DatePart(DateInterval.WeekOfYear, New DateTime(dtDate.Year, dtDate.Month, 1), FirstDayOfWeek.System)
        Return DateWeekNumber - MonthStartWeek + 1
    End Function

End Module

现在您可以在项目中的每个日期对象上使用 .GetWeekInMonth。

Dim weeknr As Integer = CDate("2013-05-31").GetWeekInMonth
于 2013-05-30T09:57:33.953 回答