是否有相当于在Excel NETWORKDAYS .NET方法?方法、Excel、NETWORKDAYS、NET

2023-09-04 05:22:35 作者:哀家要三宫六院七十二男妃

我试图做一些日期的功能在Visual Basic中。在Excel中的 NETWORKDAYS 功能正是我所需要的。是否有相同或类似的方法可以在.NET?

I'm trying to do some date math in Visual Basic. The NETWORKDAYS function in Excel is exactly what I need. Is there an equivalent or similar method available in .NET?

推荐答案

看看这个:

private int CountWorkDays( DateTime startDate, DateTime endDate, List<DateTime> excludedDates )
{
    int dayCount = 0;
    int inc = 1;
    bool endDateIsInPast = startDate > endDate;
    DateTime tmpDate = startDate;
    DateTime finiDate = endDate;

    if( endDateIsInPast )
    {
        // Swap dates around
        tmpDate = endDate;
        finiDate = startDate;

        // Set increment value to -1, so it DayCount decrements rather 
        // than increments
        inc = -1;
    }

    while( tmpDate <= finiDate )
    {
        if( !excludedDates.Contains( tmpDate ) )
        {
            dayCount += inc;
        }

        // Move onto next day
        tmpDate = tmpDate.AddDays( 1 );
    }

    return dayCount;
}