获得一个指定星期的起始和终止日期

来源:互联网 发布:培训数控编程要多少钱 编辑:程序博客网 时间:2024/06/05 02:04

获得一个指定星期的起始和终止日期
在做一个项目的时候,
一、需要得到给定日期所属的星期,按星期统计。
二、然后得到这个星期的起始终止日期。
好像没有找到现成的方法,只能自己做一个。
觉得效率差了些,时间复杂度为O(365),不知道是否有更好的方法。

        [TestMethod]
        [Description("测试某星期的其实终止日期")]
        public void testWeekDateScop()
        {
            //就测试这天了
            DateTime testDate = new DateTime(2005, 8, 28);
           
            //选择合适的日历
            System.Globalization.Calendar calendar = new System.Globalization.GregorianCalendar();

            //把指定日期转换成年和所在星期
            int theYear = calendar.GetYear(testDate);
            int theWeek = calendar.GetWeekOfYear(testDate, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Monday);

            //得到指定星期的起始终止日期
            DateTime fromDate = DateTime.MinValue;
            DateTime toDate = DateTime.MinValue ;

            //从一年的第一天到最后一天
            for ( int i = 0; i < calendar.GetDaysInYear(theYear); i++ )
            {
                //一年的第一天,到指定距离的日期
                DateTime thisDay = calendar.AddDays(new DateTime(theYear, 1, 1), i);
                //此处要和取得星期的策略一样(theWeek值)
                int curWeek = calendar.GetWeekOfYear(thisDay, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Monday);
                if ( curWeek == theWeek )
                {
                    if ( fromDate == DateTime.MinValue ) fromDate = thisDay;
                    toDate = thisDay;
                }
            }

            TestContext.WriteLine("{0}年  第{1}周 星期:{4}  起始日期:{2}  终止日期:{3}", theYear, theWeek, fromDate, toDate, calendar.GetDayOfWeek(testDate).ToString());
        }

改进一下,复杂度变为,最好O(7),最差为(365),这只能这样了
                if ( curWeek == theWeek )
                {
                    if ( fromDate == DateTime.MinValue ) fromDate = thisDay;
                    toDate = thisDay;
                }
                else if ( fromDate != DateTime.MinValue )
                    break;

结果:
2005年 
第35周
星期:Sunday 
起始日期:2005-8-22 0:00:00 
终止日期:2005-8-28 0:00:00
posted on 2005-07-30 20:57 大尾巴狼 阅读(1005) 评论(9)  编辑 收藏 收藏至365Key 所属分类: dotNet技术和知识

 
评论
# re: 获得一个指定星期的起始和终止日期 2005-07-30 21:17 zitiger
//得到指定星期的起始终止日期
DateTime fromDate = DateTime.MinValue;
DateTime toDate = DateTime.MinValue ;

这个注释是误导,这里只是定义.  回复   

# re: 获得一个指定星期的起始和终止日期 2005-07-30 21:47 大尾巴狼
呵呵
往近了看,的确注释有问题。
往远了看,是对下面多有内容的说明。
这个注释样式有误导之嫌 :)  回复   

# re: 获得一个指定星期的起始和终止日期 2005-07-30 22:58 THIN
我以前也做过这个,
先得到这天是星期几,再去加减一下就是这周第一天和最后一天.  回复   

# re: 获得一个指定星期的起始和终止日期 2005-07-31 08:42 zitiger
@THIN
你的方法很好,很简单.  回复   

# re: 获得一个指定星期的起始和终止日期 2005-07-31 09:44 zitiger

   //DateTime TestDate = new DateTime(2005, 8, 28);
   DateTime TestDate = DateTime.Today;
   //选择合适的日历
   System.Globalization.Calendar calendar = new System.Globalization.GregorianCalendar();

   //把指定日期转换成所在星期(第几个星期)
   int Week = calendar.GetWeekOfYear(TestDate, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Monday);

   //因为要求星期一作为一星期的第一天,星期天作为星期的最后一天,所以要处理一下.
   int Days = (int)TestDate.DayOfWeek ;
   if(Days == 0)
    Days = 6;
   else
    Days -=2;

   //得到指定星期的起始终止日期
   DateTime FromDate = TestDate.AddDays(-Days);
   DateTime ToDate = FromDate.AddDays(6) ;
  
   Console.WriteLine(" 第{0}周 星期:{1}  起始日期:{2}  终止日期:{3}", Week, TestDate.DayOfWeek.ToString(), FromDate, ToDate);


  回复   

# re: 获得一个指定星期的起始和终止日期 2005-07-31 13:57 大尾巴狼
恩,简单!!!!
有个小问题,最好统一用日历对象来获得相应的参数,
这样前后概念能够统一。

系统内还有个限制条件:星期的起始和终止日期,不能跨年。
if (FromDate.Year<TestDate.Year)
FromDate=new DateTime(TestDate.Year,1,1);
if(ToDate.Year>TestDate.Year)
ToDate=(new DateTime(TestDate.Year+1,1,1)).AddDays(-1);  回复   

# re: 获得一个指定星期的起始和终止日期 2005-07-31 23:51 Goodspeed

//就测试这天了
        DateTime testDate = DateTime.Now;

        //testDate = new DateTime(2005, 7, 22);

        //足够小的基准天(必须是星期一)
        DateTime baseDate = new DateTime(1989,7,3);

        TimeSpan ts = testDate - baseDate;

        int i = Convert.ToInt32(Math.Floor((ts.TotalDays) / 7));

       
        baseDate = baseDate.AddDays(i*7); //本周一
        baseDate = baseDate.AddDays(6); //本周末

  回复   

# re: 获得一个指定星期的起始和终止日期 2006-03-16 10:34 xfrenqi
/// <summary>
/// 返回指定日期 所在周的第一天 周一为每周第一天
/// </summary>
/// <param name="dt">日期</param>
/// <returns></returns>
public static DateTime FirstDayOfWeek(DateTime dt)
{
DayOfWeek dayOfWeek = dt.DayOfWeek;
if (dayOfWeek == DayOfWeek.Sunday)
{
dt = dt.AddDays(-6);
}
else
{
dt = dt.AddDays(1-(int)dayOfWeek);
}
dt = new DateTime(dt.Year,dt.Month,dt.Day,0,0,0,0);
return dt;
}

/// <summary>
/// 返回指定日期 所在周的最后一天
/// </summary>
/// <param name="dt">日期</param>
/// <returns></returns>
public static DateTime LastDayOfWeek(DateTime dt)
{
dt = FirstDayOfWeek(dt);
dt.AddDays(6);
return dt;
}

 

原创粉丝点击