计算当前月份的工作日数,不包括.net中Windows应用程序中的星期日

时间:2010-12-02 04:08:09

标签: visual-studio-2008

计算.net.please中windows应用程序中不包括星期日当天的工作日数。任何人都尽快帮助。

2 个答案:

答案 0 :(得分:1)

这将计算一个月内非星期日的数字(示例显示当月)。

            var daysThisMonthThatAreNotSundays =
            Enumerable.Range(1, DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month)).Where(
                d => new DateTime(DateTime.Now.Year, DateTime.Now.Month, d).DayOfWeek != DayOfWeek.Sunday).Count();

答案 1 :(得分:0)

这个想法是排除非工作日。这是一个基本的例子:

// define non working days of week
var nonWorkingDaysOfWeek = new List<DayOfWeek>
   {
       DayOfWeek.Sunday // hard-coded for example
   };

// define specific non-working dates
var holidays = new List<DateTime>
   {
       new DateTime(2010, 12, 25) // hard-coded for example
   };

// tally the working days
var currentYear = 2010; // hard-coded for example
var currentMonth = 12; // hard-coded for example
var daysInCurrentMonth = DateTime.DaysInMonth(currentYear, currentMonth);
var numberOfWorkingDays = 0;
for (var day = 1; day <= daysInCurrentMonth; day++)
{
    var date = new DateTime(currentYear, currentMonth, day);
    if (!nonWorkingDaysOfWeek.Contains(date.DayOfWeek) && !holidays.Contains(date))
    {
        numberOfWorkingDays++;
    }
}