在 VB 中获取从给定日期开始计算的一年中的周数


在 vb 中,我们可以从 1 月开始计算周数。 例如 1 月 1 日是第 1 周,

2 月 1 日是第 5 周,依此类推。DatePart(DateInterval.WeekOfYear, Now)

我需要计算给定日期的周数。 例如:如果将基数设置为 7 月 1 日,那么 7 月 1 日是第 1 周,基于此,10 月 3 日的周数是多少? 如何在 VB 中执行此操作?

您可以使用Calendar.GetWeekOfYear .

下面是一个示例

Dim dateNow = DateTime.Now
Dim dfi = DateTimeFormatInfo.CurrentInfo
Dim calendar = dfi.Calendar
' using Thursday because I can.
Dim weekOfyear = calendar.GetWeekOfYear(
    dateNow, _
    dfi.CalendarWeekRule, _
    DayOfWeek.Thursday) 

这样的事情应该可以解决问题:

Private Function GetWeekNumber(ByVal startMonth As Integer, ByVal startDay As Integer, ByVal endDate As Date) As Integer
    Dim span As TimeSpan = endDate - New Date(endDate.Year, startMonth, startDay)
    Return (CInt(span.TotalDays)  7) + 1
End Function

最新更新