我试图确定两个日期之间有多少个星期一。 这些日期将由datetimepicker
插入我知道有类似的线程,但它们都是PHP或VBScript。
有什么想法吗?
答案 0 :(得分:0)
这篇文章告诉你如何在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