ASP.Net C# 计算日期间工作日天数

网上搜到的,还不错,就是特殊日期这边如果需要精确的话,需要建立一个特殊日期表格才行。
protected void Button1_Click(object sender, EventArgs e)
{
    DateTime dt1 = DateTime.Now;
    DateTime dt2 = Convert.ToDateTime(TextBox2.Text.Trim());
    int i = 0;
    for (DateTime d = dt1; d <= dt2; d = d.AddDays(1))
    {
        if (!IsWeekEnd(d) && !IsSpecDay(d))
        {
            i++;
        }
    }
    i = i - 1;
    Label1.Text = i.ToString();
}

static bool IsWeekEnd(DateTime d)
{
    return d.DayOfWeek == DayOfWeek.Saturday
        || d.DayOfWeek == DayOfWeek.Sunday;
}

static bool IsSpecDay(DateTime d)
{
    return (d >= new DateTime(d.Year, 5, 1) && d <= new DateTime(d.Year, 5, 7))
        || (d >= new DateTime(d.Year, 10, 1) && d <= new DateTime(d.Year, 10, 7))
        || d == new DateTime(d.Year, 1, 1);
}

评论关闭

return top