Private Function CountWeekEnds(ByVal BeginDate As Date, ByVal EndDate As Date) As Integer
Dim Days As Integer
Do While BeginDate < EndDate
If BeginDate.DayOfWeek = DayOfWeek.Saturday OrElse BeginDate.DayOfWeek = DayOfWeek.Sunday Then
Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show(GetWeekEndDays(Me.DateTimePicker1.Value, Me.DateTimePicker2.Value))
End Sub
Public Function GetWeekEndDays(ByVal SD As Date, ByVal ED As Date) As Integer
Dim DaysDiff As Integer = DateDiff(DateInterval.Day, SD, ED)
Dim NumWeekEndDays As Integer
NumWeekEndDays = CInt((DaysDiff / 7) * 2)
If SD.DayOfWeek = DayOfWeek.Saturday Or SD.DayOfWeek = DayOfWeek.Sunday _
Or ED.DayOfWeek = DayOfWeek.Saturday Or ED.DayOfWeek = DayOfWeek.Sunday Then
NumWeekEndDays += 1
End If
Return NumWeekEndDays
End Function
I did test the code...Can you please give me sample dates that would produce errouneous results
Even if I reversed the dats I am getting a negitive but the actual number of weekend days seems to be correct...but the proof is in the pudding...all we need is one sample where it produces the wrong answer
How to Calculate number of weekend days between the two dates?
ttad
Dim BeginDate As Date = New Date(2006, 8, 16)
Dim EndDate As Date = New Date(2006, 8, 28)
MsgBox("Weekend days:" & CountWeekEnds(BeginDate, EndDate).ToString)
End Sub
Private Function CountWeekEnds(ByVal BeginDate As Date, ByVal EndDate As Date) As Integer Dim Days As Integer Do While BeginDate < EndDate If BeginDate.DayOfWeek = DayOfWeek.Saturday OrElse BeginDate.DayOfWeek = DayOfWeek.Sunday ThenDays += 1
End IfBeginDate = BeginDate.AddDays(1)
Loop Return Days End FunctionLittle_Dice
Mark Kaplan
Or a non looping suggestion:
Private
Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click MessageBox.Show(GetWeekEndDays(Me.DateTimePicker1.Value, Me.DateTimePicker2.Value)) End Sub Public Function GetWeekEndDays(ByVal SD As Date, ByVal ED As Date) As Integer Dim DaysDiff As Integer = DateDiff(DateInterval.Day, SD, ED) Dim NumWeekEndDays As Integer NumWeekEndDays = CInt((DaysDiff / 7) * 2) If SD.DayOfWeek = DayOfWeek.Saturday Or SD.DayOfWeek = DayOfWeek.Sunday _ Or ED.DayOfWeek = DayOfWeek.Saturday Or ED.DayOfWeek = DayOfWeek.Sunday Then NumWeekEndDays += 1 End If Return NumWeekEndDays End FunctionAshley Lessard
I did test the code...Can you please give me sample dates that would produce errouneous results
Even if I reversed the dats I am getting a negitive but the actual number of weekend days seems to be correct...but the proof is in the pudding...all we need is one sample where it produces the wrong answer
K-2
Stuart Robinson
Can you please give me sample dates that would produce errouneous results
8/26/06 - 8/27/06.
rnv