据当前时间获取本学期周次

 已经设定好了本学期第一个星期星期一的日期,怎样根据当前时间获取获取周次呢

DateTime dtime=DateTime.Now;

        string datetime = "2011-3-1";//本学期第一个星期一的日期

        TimeSpan ts1 = new TimeSpan(dtime.Ticks);
        TimeSpan ts2 = new TimeSpan(Convert.ToDateTime(datetime).Ticks);
        TimeSpan ts = ts2.Subtract(ts1).Duration();

        //确定此时间在一年中的位置
        int dayOfYear = ts.Days;
        //本学期第一天
        DateTime tempDate = new DateTime(Convert.ToDateTime(datetime).Year, Convert.ToDateTime(datetime).Month, Convert.ToDateTime(datetime).Day);

        int tempDayOfWeek = (int)tempDate.DayOfWeek;
        tempDayOfWeek = tempDayOfWeek == 0 ? 7 : tempDayOfWeek;
        ////确定星期几
        int index = (int)dtime.DayOfWeek;
        index = index == 0 ? 7 : index;

        //当前周的范围
        DateTime retStartDay = dtime.AddDays(-(index - 1));
        DateTime retEndDay = dtime.AddDays(7 - index);

        //确定当前是第几周
        int weekIndex = (int)Math.Ceiling(((double)dayOfYear + tempDayOfWeek) / 7);
        Response.Write("当前是第"+weekIndex+"周");

 

http://wenku.baidu.com/view/f23f9b67f5335a8102d22014.html
http://hi.baidu.com/yssc/blog/item/db24d1580e68c68d810a18ee.html

http://www.cnblogs.com/wulg/archive/2011/03/17/1986994.html

你可能感兴趣的:(据当前时间获取本学期周次)