Workday Date Calculations

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Wayne

    Workday Date Calculations

    Hi all

    I'm trying to calculate the number of days (or workdays) between 2 given
    dates that do not include weekend days or public holidays
    (public holidays are user defined from a dbase, have a start date & an end
    date & may span a weekend)
    If a start date (workday) & an end date (workday) are on the same day then
    the number of workdays will equal zero

    Some eg's.

    Start Date - 2-Dec-04 (Thurs)
    End date 2-Dec-04 (Thurs)
    No of working days = 0

    Start Date - 2-Dec-04 (Thurs)
    End date 3-Dec-04 (Fri)
    No of working days = 1

    Start Date - 2-Dec-04 (Thurs)
    End date - 10-Dec-04 (Fri)
    No of working days = 6 (8 total days minus 2 weekend days)

    Start Date - 2-Dec-04 (Thurs)
    End date - 22-Dec-04 (Wed)
    Public Holiday Start Date - 17-Dec-04 (Fri)
    Public Holiday End Date - 20-Dec-04 (Mon)
    No of working days = 12 (20 total days minus 6 weekend days minus only 2 of
    the Public Holiday days as 2 fell on a weekend)

    Start Date - 2-Dec-04 (Thurs)
    End date - 17-Dec-04 (Fri)
    Public Holiday Start Date - 17-Dec-04 (Fri)
    Public Holiday End Date - 20-Dec-04 (Mon)
    No of working days = 10 (15 total days minus 4 weekend days minus only 1 of
    the Public Holiday days as the End date fell on the 1st day of the Public
    Holday)

    I've been experimenting with timespans & datediff but just can't seem to get
    it off the ground

    Has anyone done something similar or could point me in the right direction ?

    Regards
    Wayne



  • Cor Ligthert

    #2
    Re: Workday Date Calculations

    Wayne,

    Really fun stuff, I assume (hope for you) you get a lot of better sollutions
    than this.

    This is quickly made, when you go in the direction of the timespan, know
    than that it has a limited value.

    Hollidays are not in the Framework. However they are in office.

    I made a table with one holliday and because this year chrismass as well as
    newyear are on saterday sunday I took tomorrow as a holliday in this sample

    \\\start time sample is 2004, dec, 2
    Public Class Hello
    Public Shared Sub main()
    Dim DateToReach As DateTime = New DateTime(2005, 1, 1)
    Dim hollidays(0) As DateTime
    hollidays(0) = New DateTime(2004, 12, 3) 'just for the test
    Dim count As Integer
    Dim Testdate As DateTime = Now
    Do Until Testdate.Date = DateToReach.Dat e
    If Testdate.DayOfW eek <> DayOfWeek.Sunda y AndAlso _
    Testdate.DayOfW eek <> DayOfWeek.Satur day Then
    Dim holliday As Boolean = False
    For Each td As Date In hollidays
    If Testdate.Date = td.Date Then
    holliday = True
    Exit For
    End If
    Next
    If holliday = False Then
    count += 1
    End If
    End If
    Testdate = Testdate.AddDay s(1)
    Loop
    MessageBox.Show (count.ToString )
    End Sub
    ///

    I hope this helps something?

    Cor

    "Wayne" <wgr101@hotmail .com>
    [color=blue]
    > Hi all
    >
    > I'm trying to calculate the number of days (or workdays) between 2 given
    > dates that do not include weekend days or public holidays
    > (public holidays are user defined from a dbase, have a start date & an end
    > date & may span a weekend)
    > If a start date (workday) & an end date (workday) are on the same day then
    > the number of workdays will equal zero
    >
    > Some eg's.
    >
    > Start Date - 2-Dec-04 (Thurs)
    > End date 2-Dec-04 (Thurs)
    > No of working days = 0
    >
    > Start Date - 2-Dec-04 (Thurs)
    > End date 3-Dec-04 (Fri)
    > No of working days = 1
    >
    > Start Date - 2-Dec-04 (Thurs)
    > End date - 10-Dec-04 (Fri)
    > No of working days = 6 (8 total days minus 2 weekend days)
    >
    > Start Date - 2-Dec-04 (Thurs)
    > End date - 22-Dec-04 (Wed)
    > Public Holiday Start Date - 17-Dec-04 (Fri)
    > Public Holiday End Date - 20-Dec-04 (Mon)
    > No of working days = 12 (20 total days minus 6 weekend days minus only 2
    > of
    > the Public Holiday days as 2 fell on a weekend)
    >
    > Start Date - 2-Dec-04 (Thurs)
    > End date - 17-Dec-04 (Fri)
    > Public Holiday Start Date - 17-Dec-04 (Fri)
    > Public Holiday End Date - 20-Dec-04 (Mon)
    > No of working days = 10 (15 total days minus 4 weekend days minus only 1
    > of
    > the Public Holiday days as the End date fell on the 1st day of the Public
    > Holday)
    >
    > I've been experimenting with timespans & datediff but just can't seem to
    > get
    > it off the ground
    >
    > Has anyone done something similar or could point me in the right direction
    > ?
    >
    > Regards
    > Wayne
    >
    >
    >[/color]


    Comment

    Working...