.NET Date Compare: Count the amount of working days since a date?

前端 未结 7 1138
别那么骄傲
别那么骄傲 2020-12-03 08:16

What\'s the easiest way to compute the amount of working days since a date? VB.NET preferred, but C# is okay.

And by \"working days\", I mean all days excluding Satu

7条回答
  •  情歌与酒
    2020-12-03 09:07

    This'll do what you want it to. It should be easy enough to convert to VB.NET, it's been too long for me to be able to do it though.

    DateTime start = DateTime.Now;
    DateTime end = start.AddDays(9);
    IEnumerable holidays = new DateTime[0];
    
    // basic data
    int days = (int)(end - start).TotalDays;
    int weeks = days / 7;
    
    // check for a weekend in a partial week from start.
    if (7- (days % 7) <= (int)start.DayOfWeek)
        days--;
    if (7- (days % 7) <= (int)start.DayOfWeek)
        days--;
    
    // lose the weekends
    days -= weeks * 2;
    
    foreach (DateTime dt in holidays)
    {
        if (dt > start && dt < end)
            days--;
    }
    

提交回复
热议问题