游戏技术文章

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

时间:2016-12-5 11:53:23  作者:棋牌资源网  来源:棋牌资源网  查看:7992  评论:0
内容摘要: [csharp] view plaincopyprint? public class DateTimeTool { /// /// 获取指定日期所在周的第一天,星期天为第一...
 
[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);  
          }  
      } 
 

 

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

欢迎加入VIP,【VIP售价:只要288元永久VIP会员】畅享商业棋牌游戏程序下载,点击开通!

下载说明


☉本站所有源码和资源均由站长亲自测试-绝对保证都可以架设,运营!
☉如源码和资源有损坏或所有链接均不能下载,请告知管理员,

☉本站软件和源码大部分为站长独资,资源购买和收集,放心下载!

☉唯一站长QQ:1004003180  [人格担保-本站注重诚信!]

☉购买建议E-mail:1004003180@qq.com   源码收购 E-mail:1004003180@qq.com    

☉本站文件解压密码  【文章内都自带解压密码,每个密码不同!】


本站提供的所有源码,均来源站长提供,仅学习交流 浙ICP备09009969号

由此产生不良后果和法律责任与本站无关,如果侵犯了您的版权,请来信告知 1004003180@qq.com 将及时更正和删除! 

Copyright © 2008-2022 棋牌资源网,你身边的棋牌资源下载站    All Rights Reserved