算一年中,每个星期的日期范围

 static List<object> WeekYearStr(int year)
        {
            DateTime firstDay = DateTime.Parse(year + "-1-1");
            int countWeek = WeekYear(year);
            object dayofweek = firstDay.DayOfWeek;
            Console.WriteLine("第一天是星期:" + Convert.ToString(dayofweek));
            int weeks = Convert.ToInt32(dayofweek);
            List<object> list = new List<object>();
            if (weeks == 0)//则是星期天
            {
                firstDay = firstDay.AddDays(-6);//把第一天往后倒日期
                countWeek++;//同时需要算多一个星期
            }
            else
            {
                firstDay = firstDay.AddDays(1 - weeks);//把日期往前移动一天,按星期一是开始到星期天算一个星期
            }
            for (int i = 1; i <= countWeek; i++)
            {
                KeyValue kv = new KeyValue();
                kv.key = i;
                kv.value = firstDay;
                list.Add(kv);
                firstDay = firstDay.AddDays(7);
            }
            return list;
        }

 

 

请高手指正哈。

posted on 2013-02-04 15:18  Arvin Lai  阅读(189)  评论(0编辑  收藏  举报