Counting regular working days in a given period of time

后端 未结 8 424
庸人自扰
庸人自扰 2021-01-12 05:38

need some help. I need to count regular working days for a given date period, for example, in our country, we have 5 regular working days monday to friday, then in code i ne

相关标签:
8条回答
  • 2021-01-12 06:13

    Check out this example on Code Project that uses a very efficient way that doesn't involve any looping ;)

    It uses this alogrithm:

    1. Calculate the number of time span in terms of weeks. Call it, W.
    2. Deduct the first week from the number of weeks. W= W-1
    3. Multiply the number of weeks with the number of working days per week. Call it, D.
    4. Find out the holidays during the specified time span. Call it, H.
    5. Calculate the days in the first week. Call it, SD.
    6. Calculate the days in the last week. Call it, ED.
    7. Sum up all the days. BD = D + SD + ED - H.
    0 讨论(0)
  • 2021-01-12 06:13
    int count = 0;
    switch (dateTimePicker2.Value.DayOfWeek.ToString())
    {
        case "Saturday": count--; break;
        case "Sunday": count--; break;
        default:break;
    }
    switch (dateTimePicker3.Value.DayOfWeek.ToString())
    {
        case "Saturday": count--; break;
        case "Sunday": count--; break;
        default:break;
    }
    if (count == -2)
        count = -1;
    int weeks = t.Days / 7;
    int daycount =count+ t.Days - (2 * weeks)+1;
    label7.Text = "No of Days : " + daycount.ToString();
    
    0 讨论(0)
提交回复
热议问题