本文来自CSDN博客,http://blog.csdn.net/guoyz_1/archive/2009/01/15/3785705.aspx

 

DateTime dt = DateTime.Now;  //当前时间

DateTime startWeek = dt.AddDays(1 - Convert.ToInt32(dt.DayOfWeek.ToString("d")));  //本周周一
DateTime endWeek = startWeek.AddDays(6);  //本周周日

 DateTime startMonth = dt.AddDays(1 - dt.Day);  //本月月初
DateTime endMonth = startMonth.AddMonths(1).AddDays(-1);  //本月月末
//DateTime endMonth = startMonth.AddDays((dt.AddMonths(1) - dt).Days - 1);  //本月月末

DateTime startQuarter = dt.AddMonths(0 - (dt.Month - 1) % 3).AddDays(1 - dt.Day);  //本季度初
DateTime endQuarter = startQuarter.AddMonths(3).AddDays(-1);  //本季度末

DateTime startYear = new DateTime(dt.Year, 1, 1);  //本年年初
DateTime endYear = new DateTime(dt.Year, 12, 31);  //本年年末

 

 第几周:

public int GetWeekOfCurrDate(DateTime dt)

    {

      int Week = 1;

      int nYear = dt.Year;

       System.DateTime FirstDayInYear = new DateTime(nYear, 1, 1);

       System.DateTime LastDayInYear = new DateTime(nYear, 12, 31);

      int DaysOfYear = Convert.ToInt32(LastDayInYear.DayOfYear);

      int WeekNow = Convert.ToInt32(FirstDayInYear.DayOfWeek) - 1;

      if (WeekNow < 0) WeekNow = 6;

      int DayAdd = 6 - WeekNow;

       System.DateTime BeginDayOfWeek = new DateTime(nYear, 1, 1);

       System.DateTime EndDayOfWeek = BeginDayOfWeek.AddDays(DayAdd);

       Week = 2;

      for (int i = DayAdd + 1; i <= DaysOfYear; i++)

      {

         BeginDayOfWeek = FirstDayInYear.AddDays(i);

        if (i + 6 > DaysOfYear)

        {

           EndDayOfWeek = BeginDayOfWeek.AddDays(DaysOfYear - i - 1);

         }

        else

        {

           EndDayOfWeek = BeginDayOfWeek.AddDays(6);

         }

        if (dt.Month == EndDayOfWeek.Month && dt.Day <= EndDayOfWeek.Day)

        {

          break;

         }

         Week++;

         i = i + 6;

       }

      return Week;

     }

 

posted on 2010-01-16 15:25  小傻瓜  阅读(595)  评论(0编辑  收藏  举报