比如现在List<DateTime>中存放的是:2008年8月1日
2008年8月4日
2008年8月5日
2008年8月7日
2008年8月11日
2008年8月12日
2008年8月13日
2008年8月15日
2008年8月18日
2008年8月19日
2008年8月21日
2008年8月22日
2008年8月27日
2008年8月29日请问现在如何将它按周时间段及不连续时间段划分.列如:开始时间:2008年8月1日结束时间:2008年8月1日 开始时间:2008年8月4日
结束时间:2008年8月5日 开始时间:2008年8月7日结束时间:2008年8月7日 开始时间:2008年8月11日结束时间:2008年8月13日....

解决方案 »

  1.   

    那你就foreach从头开始取第一个记为开始,往后找,找到第一个时间段的尾,记为结束时候,指针++,记为开始,继续找结束... ...
      

  2.   

    明白了,就是说:第二个和第一个 日期不连续 就分到两个组里

    第二个和第一个 虽然连续,但是属于两个周,就分到两个组里

    第二个和第一个 中间有假日,就分到两个组里
    其他
    分到一个组里;但是,你的list中有没有 周六周日或假日,如果没有好办(不就自然分组了吗?),如果有如何处理?
      

  3.   

    list中已经去除了周六周日或假日,该判断日期不连续情况和是否属于同一周里
      

  4.   

    给你写了个代码,应该还有精简的地方,你自己试试吧.变量简单说了一下,你可按自己的需要改.private void CheckListDate()
            {
                List<DateTime> l = new List<DateTime>{
                Convert.ToDateTime("2008年8月1日"),
                Convert.ToDateTime("2008年8月4日"),
                Convert.ToDateTime("2008年8月5日"),
                Convert.ToDateTime("2008年8月7日"),
                Convert.ToDateTime("2008年8月11日"),
                Convert.ToDateTime("2008年8月12日"),
                Convert.ToDateTime("2008年8月13日"),
                Convert.ToDateTime("2008年8月15日"),
                Convert.ToDateTime("2008年8月18日"),
                Convert.ToDateTime("2008年8月19日"),
                Convert.ToDateTime("2008年8月21日"),
                Convert.ToDateTime("2008年8月22日"),
                Convert.ToDateTime("2008年8月27日"),
                Convert.ToDateTime("2008年8月29日")  };
     
                int week = 0;   //记录某日期是第几周
                DateTime t = DateTime.Now;   //保存上个日期
                bool start = false;   //是否重新开始一个周期的计算
                foreach (DateTime dt in l)
                {
                    if (!start)
                    {
                        Console.WriteLine("开始时间:" + dt.ToString("yyyy年MM月dd日"));
                        week = GetWeekOfYear(dt);
                        t = dt;
                        start = true;
                    }
                    else
                    {
                        if (dt.CompareTo(t.AddDays(1)) != 0)
                        {
                            Console.WriteLine("结束时间:" + t.ToString("yyyy年MM月dd日") + "\r\n");
                            Console.WriteLine("开始时间:" + dt.ToString("yyyy年MM月dd日"));
                            week = GetWeekOfYear(dt);
                            t = dt;
                            start = true;
                            continue;
                        }
                        if (GetWeekOfYear(dt) == week)
                        {
                            t = dt;
                            continue;
                        }
                        Console.WriteLine("结束时间:" + dt.ToString("yyyy年MM月dd日") + "\r\n");
                        start = false;
                    }
                }
                Console.WriteLine("结束时间:" + t.ToString("yyyy年MM月dd日") + "\r\n");
            }        private int GetWeekOfYear(DateTime dt)
            {
                GregorianCalendar gc = new GregorianCalendar();
                return gc.GetWeekOfYear(dt, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
            }
      

  5.   

    对了,开始要加上 using System.Globalization;