本文介绍了是否有相当于在Excel NETWORKDAYS .NET方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一些日期的功能在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;
}

这篇关于是否有相当于在Excel NETWORKDAYS .NET方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 00:07