日期:2014-05-18  浏览次数:20909 次

《求教》如何判断 两个日期之间有几个 周末???
C# 如何实现啊????

------解决方案--------------------
 参考如下代码:[color=#FFFFFF][img=http://p.blog.csdn.net/images/p_blog_csdn_net/zswang/reply.jpg][/img][/color]
C# code
public int TotalWorkday(DateTime AStart, DateTime AEnd)
{
    TimeSpan vTimeSpan = new TimeSpan(AEnd.Ticks - AStart.Ticks);
    // 跨度多少个星期,每个星期五个工作日
    int Result = ((int)vTimeSpan.TotalDays / 7) * 5; 

    // 计算余下的日期中的工作日
    for (int i = 0; i <= vTimeSpan.TotalDays % 7; i++)
        switch (AStart.AddDays(i).DayOfWeek)
        {
            case DayOfWeek.Saturday:
            case DayOfWeek.Sunday:
                break;
            default:
                Result++;
                break;
        }
    return Result;
} /* TotalWorkday */

private void button1_Click(object sender, EventArgs e)
{
    Text = TotalWorkday(new DateTime(2007, 9, 1), 
        new DateTime(2007, 10, 1)).ToString();
}

------解决方案--------------------
int weeks = new System.Globalization.GregorianCalendar().GetWeekOfYear(System.DateTime.Now, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Sunday);

int weeks1 = new System.Globalization.GregorianCalendar().GetWeekOfYear(System.DateTime.Now.AddDays(100), System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Sunday);

int week=weeks1-week;