• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
[csharp] view plaincopyprint?

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

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#调用存储过程中事务级临时表返回DataTable列乱序解决办法发布时间:2022-07-18
下一篇:
c++中sizeof的用法发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap