Hi, i have problem when counting between datetimepicker
here is the data on database
PROJECT
========
START_DATE
END_DATE
TOTAL_DAY
and I have 2 Datetimepicker on VB.NET
Datetimepicker1 ---> contains START_DATE
Datetimepicker2 ---> contains END_DATE
txtTotal ---> will be automatically generated once both Datetimepicker are selected and contains formula END_DATE - START_DATE, the result will be added to TOTAL_DAY in database
how to calculate it on VB.NET only for workdays, Saturday and Sunday doesn't need to count.
any helps would be appreciated, thx in advance :)
Loading
VulpesPosted Oct 30, 2012, 9:28 AM
mohamad gunturPosted Oct 30, 2012, 9:45 PM
Function GetWorkDays(ByVal startdate As Date, ByVal enddate As Date) As Integer
startdate = New DateTime(startdate .Year, startdate .Month, startdate .Day)
enddate = New DateTime(enddate.Year, enddate.Month, enddate.Day)
Dim workdays As Integer = 0
Dim weekdays As Integer = 0
While (startdate <= enddate )
If startdate.DayOfWeek = DayOfWeek.Saturday Or enddate.DayOfWeek = DayOfWeek.Sunday Then
workdays = workdays
Else
workdays = workdays + 1
End If
startdate = startdate.AddDays(1)
End While
Return workdays
End Function
Sub countDays()
Dim totaldays As String = GetWorkDays(DateTimePicker1.Value, DateTimePicker2.Value)
txtTOTAL_DAYS.Text = totaldays
End Sub
Private Sub DateTimePicker2_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DateTimePicker2.ValueChanged
countDays()
End Sub
well, u really helped me a lot. Thank you very much :D
mohamad gunturPosted Oct 29, 2012, 10:25 PM
i also add Me.txtTOTAL_DAYS.Text to replace the textTotal...
Dim temp As DateTime = DateTimePicker1.Value
Dim endDate As DateTime = DateTimePicker2.Value
Dim workDays As Integer = 0
While temp <= endDate
If temp.DayOfWeek <> DayOfWeek.Saturday AndAlso temp.DayOfWeek <> DayOfWeek.Sunday Then
workDays += 1
End If
temp = temp.AddDays(1)
End While
Me.txtTOTAL_DAYS.Text = workDays.ToString
the txtTOTAL_DAYS should be datetimepicker2 - datetimepicker1 (except saturday and sunday), but the value still 0, none had calculated
VulpesPosted Oct 29, 2012, 3:03 PM
Notice that the result will be inclusive of both the start and end dates.