# VB.NET - leave managment - Asked By pavan r on 08-Dec-11 06:05 AM

when i applying four half days leave .... for from date and to date using two text boxes  ........example 2-09-2011 to 5-09-2011 .... it will count and displaying in the textboxes  only two days will be display
Sakshi a replied to pavan r on 08-Dec-11 06:30 AM

Dim datTim1 As Date = #1/4/2001#
Dim datTim2 As Date = #1/9/2001#
' Assume Sunday is specified as first day of the week.
Dim wD As Long = DateDiff(DateInterval.Weekday, datTim1, datTim2)

this will give the count
Chintan Vaghela replied to pavan r on 08-Dec-11 06:48 AM

NettoWorkdays(Convert.ToDateTime("2011-09-02"), Convert.ToDateTime("2011-09-05"))

Made function as follows

` `

Public Function NettoWorkdays(ByVal dtmStart As Date, ByVal dtmEnd As Date) As Integer

'This function calculates the number of working days (monday to friday) between 2 dates,

'including the first and the last day

Dim intDays As Integer

Dim intSubtract As Integer

' if end is smaller then start return -1

If dtmEnd < dtmStart Then

NettoWorkdays = -1

Else

' Get the start and end dates to be weekdays.

While Weekday(dtmStart) = vbSaturday Or Weekday(dtmStart) = vbSunday

End While

While Weekday(dtmEnd) = vbSaturday Or Weekday(dtmEnd) = vbSunday

End While

If dtmStart > dtmEnd Then

' Sorry, no Workdays to be had. Just return 0.

NettoWorkdays = 0

Else

intDays = dtmEnd.Day - dtmStart.Day + 1

' Subtract off weekend days.  Do this by figuring out how

' many calendar weeks there are between the dates, and

' multiplying the difference by two (because there are two

' weekend days for each week). That is, if the difference

' is 0, the two days are in the same week. If the

' difference is 1, then we have two weekend days.

intSubtract = (DateDiff("ww", dtmStart, dtmEnd) * 2)

NettoWorkdays = intDays - intSubtract

End If

End If

`    End Function`

Hope this is helpful !

Thanks

Reena Jain replied to pavan r on 08-Dec-11 01:37 PM
hi,

The DateTime.Substract method may be used in order to find the date/time difference between two instances of the DateTime method. This method does not change the value of the DateTime instance on which the method has been invoked. The result of the operation is stored in the new TimeSpan structure.

`System.DateTime dtfrom = Convert.TodateTime(textbox1.Text);`
`System.DateTime dtto =Convert.TodateTime(textbox2.Text);`
`System.TimeSpan diffResult = Convert.TodateTime(textbox2.Text) - Convert.TodateTime(textbox1.Text);`
`Textbox3.Text=diffResult.ToString();`

An alternative for date subtraction in C# is by importing the Microsoft.VisualBasic namespace and invoking the DateDiff method. here is the example for this

`DateTime dt = Convert.ToDateTime(``"05/09/2011"``);`
`DateTime dt1 = Convert.ToDateTime(``"02/09/2011"``);`
`TimeSpan ts = dt.Subtract(dt1);`
`int` `day = ts.Days;`
`textBox1.Text = day.ToString();`

pradeep kumar replied to Sakshi a on 09-Dec-11 12:46 AM
i can using the radio buttonlist ... so here two radio button ... one radio button containing half day and another radio button containing full day ... and from date and to date using two text boxes ....  when i taking the 4 half day leave ... it will count and display the only two days in the label msg..... i got full days counting the days ... but i didnt get half days counting .... so plz give me the exact code for this
thank u
pradeep kumar replied to Chintan Vaghela on 09-Dec-11 12:46 AM
i can using the radio buttonlist ... so here two radio button ... one radio button containing half day and another radio button containing full day ... and from date and to date using two text boxes ....  when i taking the 4 half day leave ... it will count and display the only two days in the label msg..... i got full days counting the days ... but i didnt get half days counting .... so plz give me the exact code for this
thank u
Chintan Vaghela replied to pradeep kumar on 09-Dec-11 04:41 AM

Try following code

Made  one function as follows

Public Function NettoWorkdays(ByVal dtmStart As Date, ByVal dtmEnd As Date) As Integer

'This function calculates the number of working days (monday to friday) between 2 dates,

'including the first and the last day

Dim intDays As Integer

Dim intSubtract As Integer

' if end is smaller then start return -1

If dtmEnd < dtmStart Then

NettoWorkdays = -1

Else

' Get the start and end dates to be weekdays.

While Weekday(dtmStart) = vbSaturday Or Weekday(dtmStart) = vbSunday

End While

While Weekday(dtmEnd) = vbSaturday Or Weekday(dtmEnd) = vbSunday

End While

If dtmStart > dtmEnd Then

' Sorry, no Workdays to be had. Just return 0.

NettoWorkdays = 0

Else

intDays = DateDiff(DateInterval.Day, dtmStart, dtmEnd) + 1

' Subtract off weekend days.  Do this by figuring out how

' many calendar weeks there are between the dates, and

' multiplying the difference by two (because there are two

' weekend days for each week). That is, if the difference

' is 0, the two days are in the same week. If the

' difference is 1, then we have two weekend days.

intSubtract = (DateDiff("ww", dtmStart, dtmEnd) * 2)

NettoWorkdays = intDays - intSubtract

End If

End If

End Function

Call above function as follows

Dim LeaveDay As Double

LeaveDay = NettoWorkdays(Convert.ToDateTime("2011-12-03"), Convert.ToDateTime("2011-12-12"))

Dim LeaveType As String

LeaveType = "Half" ' Set Your RadioButton List Value Here If LeaveType 'Half' then LeaveDay/2 is Total Leave other wise LeaveDay is Total Leave

If LeaveType = "Half" Then

LeaveDay = LeaveDay / 2

End If

Check it and let me know your feedback

Hope this is helpful !

Thanks