Microsoft . 技术之路...

—— 专注于微软技术, 分享是快乐的源泉......
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

计算周次和本周时间范围的代码

Posted on 2006-11-15 15:39  赣江源  阅读(269)  评论(0编辑  收藏  举报

//当天与该周星期一相差的天数
  private int getModdayNum(System.DayOfWeek dw)
  {
   int weeknow = Convert.ToInt32(dw);
   int moddayNum = (-1) * weeknow+1;
   return moddayNum;
  }

  //当天与该周星期日相差的天数
  private int getSundayNum(System.DayOfWeek dw)
  {
   int weeknow = Convert.ToInt32(dw);
   int sundayNum = 7-weeknow;
   return sundayNum;
  }

  //该周星期一的日期
  private string getWeekMonday(System.DayOfWeek dw,string timeFormateStr)
  {
   int weeknow = Convert.ToInt32(dw);
   int moddayNum = (-1) * weeknow+1;
   string weekMonday = System.DateTime.Now.AddDays(moddayNum).Date.ToString(timeFormateStr);
   return weekMonday;
  }

  //该周星期日的日期
  private string getWeekSunday(System.DayOfWeek dw,string timeFormateStr)
  {
   int weeknow = Convert.ToInt32(dw);
   int sundayNum = 7-weeknow;
   string weekSunday = System.DateTime.Now.AddDays(sundayNum).Date.ToString(timeFormateStr);
   return weekSunday;
  }

  //本周是本年第几周
  private int weekNum(System.DayOfWeek dw)
  {
   int weeknow = Convert.ToInt32(dw);//今天星期几
   int daydiff = (-1) * (weeknow+1);//今日与上周末的天数差
   int days = System.DateTime.Now.AddDays(daydiff).DayOfYear;//上周末是本年第几天
   int weeks = days/7;
   if(days%7 != 0)
   {
    weeks++;
   }
   return (weeks+2);
  }

   //调用方法
   string datetime = getWeekMonday(System.DateTime.Now.DayOfWeek,"yyyy-MM-dd");