[英]Is there a .NET method equivalent to NETWORKDAYS in Excel?
我正在嘗試在Visual Basic中進行一些日期計算。 Excel中的NETWORKDAYS
函數正是我所需要的。 .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;
}
據我所知,但想出一個並不難。
假設周六和周日關閉,則計算當前月份中的天數,遍歷日期,如果當前日期是周日或周六,或者與假期數組中的值匹配,則減去月份中的天數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.