获取周的第一天、最后一天、月第一天和最后一天

    public class CommonTool  
      {  
          ///

 
          /// 获取指定日期所在周的第一天,星期天为第一天  
          ///
 
          ///  
          ///  
          public static DateTime GetDateTimeWeekFirstDaySun(DateTime sometime)  
          {  
              DateTime firstWeekDay = DateTime.Now;  
      
              try  
              {  
                  //得到是星期几,然后从当前日期减去相应天数   
                  int weeknow = Convert.ToInt32(sometime.DayOfWeek);  
      
                  int daydiff = (-1) * weeknow;  
      
                  firstWeekDay = sometime.AddDays(daydiff);  
              }  
              catch { }  
      
              return firstWeekDay;  
          }  
      
          ///  
          /// 获取指定日期所在周的第一天,星期一为第一天  
          ///
 
          ///  
          ///  
          public static DateTime GetDateTimeWeekFirstDayMon(DateTime sometime)  
          {  
              DateTime firstWeekDay = DateTime.Now;  
      
              try  
              {  
                  int weeknow = Convert.ToInt32(sometime.DayOfWeek);  
      
                  //星期一为第一天,weeknow等于0时,要向前推6天。     
                  weeknow = (weeknow == 0 ? (7 - 1) : (weeknow - 1));  
      
                  int daydiff = (-1) * weeknow;  
      
                  firstWeekDay = sometime.AddDays(daydiff);  
              }  
              catch { }  
      
              return firstWeekDay;  
          }  
      
          ///  
          /// 获取指定日期所在周的最后一天,星期六为最后一天  
          ///
 
          ///  
          ///  
          public static DateTime GetDateTimeWeekLastDaySat(DateTime sometime)  
          {  
              DateTime lastWeekDay = DateTime.Now;  
      
              try  
              {  
                  int weeknow = Convert.ToInt32(sometime.DayOfWeek);  
      
                  int daydiff = (7 - weeknow) - 1;  
      
                  lastWeekDay = sometime.AddDays(daydiff);  
      
              }catch { }  
      
              return lastWeekDay;  
          }  
      
          ///  
          /// 获取指定日期所在周的最后一天,星期天为最后一天  
          ///
 
          ///  
          ///  
      
          public static DateTime GetDateTimeWeekLastDaySun(DateTime sometime)  
          {  
              DateTime lastWeekDay = DateTime.Now;  
      
              try  
              {  
                  int weeknow = Convert.ToInt32(sometime.DayOfWeek);  
      
                  weeknow = (weeknow == 0 ? 7 : weeknow);  
      
                  int daydiff = (7 - weeknow);  
      
                  lastWeekDay = sometime.AddDays(daydiff);  
              }  
              catch { }  
      
              return lastWeekDay;  
          }  
      
          ///  
          /// 获取指定日期的月份第一天  
          ///
 
          ///  
          ///  
          public static DateTime GetDateTimeMonthFirstDay(DateTime sometime)  
          {  
              if (sometime == null)  
              {  
                  sometime = DateTime.Now;  
              }  
      
              return new DateTime(sometime.Year, sometime.Month, 1);  
          }  
      
          ///  
          /// 获取指定月份最后一天  
          ///
 
          ///  
          ///  
          public static DateTime GetDateTimeMonthLastDay(DateTime sometime)  
          {  
              int day = DateTime.DaysInMonth(sometime.Year, sometime.Month);  
      
              return new DateTime(sometime.Year, sometime.Month, day);  
          }  
      }

你可能感兴趣的:(C#)