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
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
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
Days += 1
End If
BeginDate = BeginDate.AddDays(1)
Loop
Return Days
End Function
How to Calculate number of weekend days between the two dates?
How to Calculate number of weekend days between the two dates?
ntsoo
Can you please give me sample dates that would produce errouneous results
8/26/06 - 8/27/06.
Lee Brimelow
Erling Ervik
Karthick1020
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
Martin Bennedik
aeonblaire
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 Functiondagfari
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 Function