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


    [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# 基础知识系列- 11 委托和事件
    C# 基础知识系列- 10 反射和泛型(二)
    C# 基础知识系列- 9 字符串的更多用法(二)
    C# 基础知识系列- 9 字符串的更多用法(一)
    C# 基础知识系列- 8 Linq最后一部分查询表达式语法实践
    C# 基础知识系列- 6 Lambda表达式和Linq简单介绍
    C# 基础知识系列- 5 反射和泛型
    C# 基础知识系列- 4 面向对象
    TLS加密远程连接Docker
    树莓派4B安装64位Linux(不用显示器键盘鼠标)
  • 原文地址:https://www.cnblogs.com/xuxiaoshuan/p/4999833.html
Copyright © 2020-2023  润新知