今天做的一个模块涉及到了一个判断工作日天数的小问题.思考了一番给出了解决方案.下面拿出来晒一下.
static void Main(string[] args)
{
int year = 2012;
int month = 9;
DateTime dateFrom = new DateTime(year, month, 1);
DateTime dateTo = new DateTime(year, month, 30);
TimeSpan ts = dateTo.AddDays(1) - dateFrom;
int days = ts.Days - ts.Days / 7 * 2;
Console.WriteLine("(1 before caculate) WorkDay count is : " + days);
if (dateFrom.DayOfWeek == DayOfWeek.Sunday || ts.Days % 7 + (int)dateFrom.DayOfWeek == 7)
{
days -= 1;
}
else if (ts.Days % 7 + (int)dateFrom.DayOfWeek > 7)
{
days -= 2;
}
Console.WriteLine("(2 after caculate) WorkDay count is : " + days);
Console.ReadKey();
}