Alec Pojidaev's InfoPath Blog
Stuff published for no reason (cool codeless infopath solutions mostly)

Work (business) days calculations with C#

If you think it’s a decent solution vote for it! :)


Work days between 2 dates:

public static double GetBusinessDays(DateTime startD, DateTime endD)

{

double calcBusinessDays =

1 + ((endD-startD).TotalDays * 5 -

(startD.DayOfWeek-endD.DayOfWeek) * 2) / 7;

if ((int)endD.DayOfWeek == 6) calcBusinessDays --;

if ((int)startD.DayOfWeek == 0) calcBusinessDays --;

return calcBusinessDays;

}


Add work days to a start date:

DateTime startD = new DateTime(2000, 5, 28);

double businessDays = 2300;

int DoW = (int)startD.DayOfWeek;

double temp = businessDays + DoW + 1;

if (DoW != 0) temp --;

DateTime calcendD = startD.AddDays(

Math.Floor(temp / 5)*2-DoW + temp

- 2* Convert.ToInt32(temp % 5 == 0)) ;

About these ads

13 Responses to “Work (business) days calculations with C#”

  1. What is the logic behind. Could you pls explain?
    thanks,
    burak.

    • I got that formula after simplifying more complex expression so I don’t know the meanings of the multiplyers. The initial formula was just math expression that anybody can construct if they have basic logic skills and familiar with concept of modulus.

  2. The above code fragment does not even compile, let alone calculate the figure suggested.

    • It’s working. The WordPress site is replacing the minus sign with something different when spaces are from both sides of the minus sign. I’ve removed spaces and now copy paste won’t get you into troubles anymore. Anyway thanks.

      The downside of that fix is this post won’t be serving as a test for people’s basic troubleshooting skills anylonger :P

  3. Very well done.
    I tried it for each month of 2010, and it is good.

    Here the convert I used in VB.NET :

    Public Shared Function GetBusinessDays(ByVal startD As DateTime, ByVal endD As DateTime) As Double
    Dim calcBusinessDays As Double = 1 + ((endD - startD).TotalDays * 5 - (startD.DayOfWeek - endD.DayOfWeek) * 2) / 7
    If CInt(endD.DayOfWeek) = 6 Then calcBusinessDays -= 1
    If CInt(startD.DayOfWeek) = 0 Then calcBusinessDays -= 1
    Return calcBusinessDays
    End Function

    Thanks

  4. It’s probably prudent to use a Math.Floor around TotalDays… I would expect a whole number of business days. Especially useful when I need to multiply by the (average/expected) number of hours worked per day.

    double calcBusinessDays =
    1 + (Math.Floor((endD-startD).TotalDays) * 5 –
    (startD.DayOfWeek-endD.DayOfWeek) * 2) / 7;

    • No need to use any rounding function here. There will be no reminder after division operation. That is some little math magic for you.

      But using rounding function as it is in your example is especially pointless (its impossible to get “not round” figures as a result of multiplication/substraction of whole numbers)

  5. Thanks alec… it works like a charm!!!

  6. This is a nice, elegant solution. Wrote up some unit tests to verify it and feel good about it. Thanks man!

  7. why adding one work day in your second part “Add work days to a start date” gives the same date as a result?

  8. First of all, great functions!

    Just wondering if you knew of a formula for work days plus Saturdays (M-Sa)? I’ve altered the work days between two dates as follows:

    public static double GetBusinessDays(DateTime startD, DateTime endD)
    {
    double calcBusinessDays =
    1 + ((endD-startD).TotalDays * 6 –
    (startD.DayOfWeek-endD.DayOfWeek)) / 7;
    if ((int)startD.DayOfWeek == 0) calcBusinessDays –;
    return calcBusinessDays;
    }

    My initial testing seems to be positive. Any feedback would be greatly appreciated. Thanks


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 40 other followers

%d bloggers like this: