• DateHelper


     public class DateHelper
        {
            #region 得到一周的周一和周日的日期
            /// <summary> 
            /// 计算本周的周一日期 
            /// </summary> 
            /// <returns></returns> 
            public static DateTime GetMondayDate()
            {
                return GetMondayDate(DateTime.Today);
            }
            /// <summary> 
            /// 计算本周周日的日期 
            /// </summary> 
            /// <returns></returns> 
            public static DateTime GetSundayDate()
            {
                return GetSundayDate(DateTime.Today);
            }
            /// <summary> 
            /// 计算某日起始日期(礼拜一的日期) 
            /// </summary> 
            /// <param name="someDate">该周中任意一天</param> 
            /// <returns>返回礼拜一日期,后面的具体时、分、秒和传入值相等</returns> 
            private static DateTime GetMondayDate(DateTime someDate)
            {
                int i = someDate.DayOfWeek - DayOfWeek.Monday;
                if (i == -1) i = 6;// i值 > = 0 ,因为枚举原因,Sunday排在最前,此时Sunday-Monday=-1,必须+7=6。 
                TimeSpan ts = new TimeSpan(i, 0, 0, 0);
                return someDate.Subtract(ts);
            }
            /// <summary> 
            /// 计算某日结束日期(礼拜日的日期) 
            /// </summary> 
            /// <param name="someDate">该周中任意一天</param> 
            /// <returns>返回礼拜日日期,后面的具体时、分、秒和传入值相等</returns> 
            private static DateTime GetSundayDate(DateTime someDate)
            {
                int i = someDate.DayOfWeek - DayOfWeek.Sunday;
                if (i != 0) i = 7 - i;// 因为枚举原因,Sunday排在最前,相减间隔要被7减。 
                TimeSpan ts = new TimeSpan(i, 0, 0, 0);
                return someDate.Add(ts);
            }
            #endregion
    
            #region 得到一月的月初和月末的日期
            /// <summary>
            /// 获得某日期月初
            /// </summary>
            /// <param name="someDate"></param>
            /// <returns></returns>
            public static DateTime GetMonthStart(DateTime someDate)
            {
                DateTime startMonth = someDate.Date.AddDays(1 - someDate.Day);  //本月月初
                return startMonth;
            }
            /// <summary>
            /// 获得某日期月末
            /// </summary>
            /// <param name="someDate"></param>
            /// <returns></returns>
            public static DateTime GetMonthEnd(DateTime someDate)
            {
                DateTime endMonth = someDate.Date.AddDays((someDate.Date.AddMonths(1) - someDate).Days - 1);  //本月月末
                return endMonth;
            }
            /// <summary>
            /// 获得本月月初
            /// </summary>
            /// <returns></returns>
            public static DateTime GetMonthStart()
            {
                return GetMonthStart(DateTime.Today);
            }
            /// <summary>
            /// 获得本月月末
            /// </summary>
            /// <returns></returns>
            public static DateTime GetMonthEnd()
            {
                return GetMonthEnd(DateTime.Today);
            }
            #endregion
    
            #region 得到一季度的季度初和季度末的日期
            /// <summary>
            /// 获得某日期季度初
            /// </summary>
            /// <param name="someDate"></param>
            /// <returns></returns>
            public static DateTime GetQuarterStart(DateTime someDate)
            {
                var dt = someDate.Date;
                DateTime startQuarter = dt.AddMonths(0 - (dt.Month - 1) % 3).AddDays(1 - dt.Day);  //本季度初
                return startQuarter;
            }
            /// <summary>
            /// 获得某日期季度末
            /// </summary>
            /// <param name="someDate"></param>
            /// <returns></returns>
            public static DateTime GetQuarterEnd(DateTime someDate)
            {
                var dt = someDate.Date;
                DateTime startQuarter = dt.AddMonths(0 - (dt.Month - 1) % 3).AddDays(1 - dt.Day);  //本季度初
                DateTime endQuarter = startQuarter.AddMonths(3).AddDays(-1);  //本季度末
                return endQuarter;
            }
            /// <summary>
            /// 获得本季度初
            /// </summary>
            /// <returns></returns>
            public static DateTime GetQuarterStart()
            {
                return GetQuarterStart(DateTime.Today);
            }
            /// <summary>
            /// 获得本季度末
            /// </summary>
            /// <returns></returns>
            public static DateTime GetQuarterEnd()
            {
                return GetQuarterEnd(DateTime.Today);
            }
            #endregion
    
    
            #region 得到一年的年初和年末日期
            /// <summary>
            /// 获得某日期年初
            /// </summary>
            /// <param name="someDate"></param>
            /// <returns></returns>
            public static DateTime GetYearStart(DateTime someDate)
            {
                DateTime startYear = new DateTime(someDate.Year, 1, 1);  //本年年初
                return startYear;
            }
            /// <summary>
            /// 获得某日期年末
            /// </summary>
            /// <param name="someDate"></param>
            /// <returns></returns>
            public static DateTime GetYearEnd(DateTime someDate)
            {
                DateTime endYear = new DateTime(someDate.Year, 12, 31);  //本年年末
                return endYear;
            }
            /// <summary>
            /// 获得本年年初
            /// </summary>
            /// <returns></returns>
            public static DateTime GetYearStart()
            {
                return GetYearStart(DateTime.Today);
            }
            /// <summary>
            /// 获得本年年末
            /// </summary>
            /// <returns></returns>
            public static DateTime GetYearEnd()
            {
                return GetYearEnd(DateTime.Today);
            }
            #endregion
        }
    

      

    慎于行,敏于思!GGGGGG
  • 相关阅读:
    HTML解决浏览器字体大小12px限制,实现自动适应大小
    Oracle 大最插入数据 一段时间之后变慢问题解决方法
    中间件使用-nginx 中ssl证书的设置
    asp.net core学习:准备asp.net core源码编译环境
    批量修改文件名后缀
    tcpdump 抓所有网卡的包
    mysql数据库备份
    x64架构下Linux系统函数调用
    博客背景美化——动态雪花飘落
    MySQL锁:03.InnoDB行锁
  • 原文地址:https://www.cnblogs.com/GarsonZhang/p/11252035.html
Copyright © 2020-2023  润新知