|
-
Jul 11th, 2012, 05:43 AM
#1
Thread Starter
Lively Member
Calculating WORKING DAYS (Mon-Fri)
Hi All,
I posted the below in the Visual Basic forum and was advised to post it here as well. Hope you can help.
I am very new to VB.net and need a little help. I currently have a calculator to calculate the number of days between two days.
What i need now is something that will calculate working days between two dates (Mon-Fri being the workdays)
(See Below)
Code:
Public Class GMPF_Calculator
Dim d1 As Date
Dim d2 As Date
Dim days As Double
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'============Date fields Start============
d1 = TextBox1.Text
d2 = TextBox2.Text
If d1 > d2 Then
MessageBox.Show("Start Date is greater than End Date", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Exit Sub
End If
days = DateDiff(DateInterval.Day, d1, d2) + 1
TextBox4.Text = days
Thanks in advance
-
Jul 11th, 2012, 06:37 AM
#2
Re: Calculating WORKING DAYS (Mon-Fri)
This StackOverflow thread is probably what you want. It also brings up a good point--will you be wanting to ignore holidays?
I can explain the math if you wish. The code is unfortunately in C#, though it can be translated directly to VB: != means <>, && means And, "x++" means "x = x + 1", ;'s can be ignored, "x -= y" means "x = x - y", etc. (It therefore does not leverage VB's DateDiff function.)
That said some kind soul may take the time to walk you through writing the algorithm yourself in VB. It's not hard, just a little tedious, and the C# code I linked does it and has already been debugged....
The time you enjoy wasting is not wasted time.
Bertrand Russell
<- Remember to rate posts you find helpful.
-
Jul 11th, 2012, 10:17 AM
#3
Thread Starter
Lively Member
Re: Calculating WORKING DAYS (Mon-Fri)
Hi Jemidiah,
Thanks for your reply. I have just had a look at the page you provided. I am new to VB.net and am unsure how to link that code with my textbox's. Is it simple?
Regards
Chris
-
Jul 11th, 2012, 06:40 PM
#4
Re: Calculating WORKING DAYS (Mon-Fri)
Yes it is simple. You want to use the DateTime.Parse method. An example:
Code:
DateTime start = DateTime.Parse("June 12 1985")
Replace the string with TextBox1.Text. You should probably validate the input (what if someone types "idontwanttotellyouwhenitstarts"?), but that's probably more appropriate for your original thread. If you accept the first post's algorithm, the math is done.
The time you enjoy wasting is not wasted time.
Bertrand Russell
<- Remember to rate posts you find helpful.
-
Aug 1st, 2012, 04:13 AM
#5
Thread Starter
Lively Member
Re: Calculating WORKING DAYS (Mon-Fri)
Hi All,
Many thanks for your replies. Sorry about the delay been very busy with work and had no time to work on this calculator. I have been trying this morning to get this working, and convert the C# from the link above into VB.
I have managed to convert it to this:-
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call CalcBusinessDays()
Dim start As Date = (tb1.Text)
Dim [end] As Date = (tb2.Text)
Dim workingDays As Integer = 0
While start < [end]
If start.DayOfWeek <> DayOfWeek.Saturday AndAlso start.DayOfWeek <> DayOfWeek.Sunday Then
workingDays = workingDays + 1
End If
start = start.AddDays(1)
End While
End Sub
End Class
But doesn't seem to work as the number of days come out wrong 60% of the time.
PLEASE PLEASE HELP!!
Regards
Chris
-
Aug 1st, 2012, 09:39 AM
#6
Re: Calculating WORKING DAYS (Mon-Fri)
That code isn't counting the last day. Change While start < [end] to While start <= [end] and it should work.
Or, use something like this:
Code:
Days = DateDiff(DateInterval.Day, StartDate, EndDate) + 1
Weeks = Days \ 7
Days = Days Mod 7
If Days > 0 Then
If StartDate.DayOfWeek = DayOfWeek.Sunday Or EndDate.DayOfWeek = DayOfWeek.Saturday Then
Days = Days - 1
ElseIf EndDate.DayOfWeek < StartDate.DayOfWeek Then
Days = Days - 2
End If
End If
Weekdays = Weeks * 5 + Days
-
Aug 1st, 2012, 10:19 AM
#7
Thread Starter
Lively Member
Re: Calculating WORKING DAYS (Mon-Fri)
Works perfect thanks Logophobic!!! 
Would I be pushing my luck to try find something that calculates the TOTAL number of working days in the month of the "start date" and total number of working days in the month "end date".
This is to calculate pay for part month e.g... 100 = monthly salary ---- leave date = 15/06/2012 ---- Total Working Days = 21 ---- working days between 01/06/2012 - leave date (15/06/2012) = 11
100 / 21 * 11 gives the part month earnings
Regards
Chris
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|