一个月有多少个星期一 Visual Basic

How many Mondays in a Month Visual Basic

我正在尝试确定两个日期之间有多少个星期一。 这些日期将由日期时间选择器插入

我知道有类似的线程,但它们都在 PHP 或 VBScript 中。

有什么想法吗?

此 post 告诉您如何在 C# 中执行此操作。 Count number of Mondays in a given date range

我通过下面的转换器获得了 运行 代码。还没有测试过。

Private Shared Function CountDays(day As DayOfWeek, start As DateTime, [end] As DateTime) As Integer

   Dim ts As TimeSpan = [end] - start

   ' Total duration
   Dim count As Integer = CInt(Math.Floor(ts.TotalDays / 7))

   ' Number of whole weeks
   Dim remainder As Integer = CInt(ts.TotalDays Mod 7)

   ' Number of remaining days
   Dim sinceLastDay As Integer = CInt([end].DayOfWeek - day)

   ' Number of days since last [day]
   If sinceLastDay < 0 Then
      sinceLastDay += 7
   End If

   ' Adjust for negative days since last [day]
   ' If the days in excess of an even week are greater than or equal to the number days since the last [day], then count this one, too.

   If remainder >= sinceLastDay Then
     count += 1
   End If

   Return count

End Function