【发布时间】:2015-02-07 22:36:17
【问题描述】:
我试图确定两个日期之间有多少个星期一。 这些日期将由 datetimepicker 插入
我知道有类似的线程,但它们都是 PHP 或 VBScript。
有什么想法吗?
【问题讨论】:
-
您的标题显示“一个月中的星期一”,但您的问题文本询问“两个日期之间有多少个星期一”。你要哪一个?
-
这两个日期是月初和月底
我试图确定两个日期之间有多少个星期一。 这些日期将由 datetimepicker 插入
我知道有类似的线程,但它们都是 PHP 或 VBScript。
有什么想法吗?
【问题讨论】:
这篇文章告诉你如何在 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
【讨论】: