Results 1 to 7 of 7

Thread: Calculating WORKING DAYS (Mon-Fri)

  1. #1
    Member
    Join Date
    Feb 12
    Posts
    48

    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

  2. #2
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 02
    Posts
    2,301

    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.

  3. #3
    Member
    Join Date
    Feb 12
    Posts
    48

    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

  4. #4
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 02
    Posts
    2,301

    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.

  5. #5
    Member
    Join Date
    Feb 12
    Posts
    48

    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

  6. #6
    Fanatic Member
    Join Date
    Jun 06
    Posts
    999

    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

  7. #7
    Member
    Join Date
    Feb 12
    Posts
    48

    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
  •