• C#获取时间段:今天,昨天,本周,上周,本月,上月,本季度,上季度,今年,去年


    参考:https://www.iteye.com/blog/xqf222-1920357

    https://blog.csdn.net/u011981242/article/details/51595546

        /// <summary>
        /// 获取时间段(某段时间的0时0分0秒-23时59分59秒)
        /// </summary>
        public class DateTimeHelper
        {
            /// <summary>
            /// 当前时间
            /// </summary>
          //public  static DateTime dt = DateTime.Now; //当前时间  
    
            //DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") //24小时制 
            //DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")  //12小时制  
    
            /// <summary>
            /// 今天开始,0时0分0秒:
            /// </summary>
            public static DateTime startCurrentDay= Convert.ToDateTime(DateTime.Now.ToString("D").ToString());
    
            /// <summary>
            /// 今天结束,23时59分59秒:
            /// </summary>
            //public static DateTime endCurrentDay = Convert.ToDateTime(DateTime.Now.AddDays(1).ToString("D").ToString()).AddSeconds(-1);
            //public static DateTime endCurrentDay = DateTime.Now.Date.AddDays(1).AddSeconds(-1);
            public static DateTime endCurrentDay = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59);
    
            /// <summary>
            /// 昨天开始
            /// </summary>
            public static DateTime startLastDay = DateTime.Now.AddDays(-1).Date;
    
            /// <summary>
            /// 昨天结束
            /// </summary>
            public static DateTime endLastDay = startLastDay.AddDays(1).AddSeconds(-1);
    
    
            /// <summary>
            /// 今天星期几
            /// </summary>
            public static int currentDayOfWeek = Convert.ToInt32(DateTime.Now.DayOfWeek.ToString("d")); 
    
            /// <summary>
            /// 本周周一
            /// </summary>
            public static DateTime startCurrentWeek = DateTime.Now.AddDays(1 - ((currentDayOfWeek == 0) ? 7 : currentDayOfWeek)).Date;  
    
            /// <summary>
            /// 本周周日
            /// </summary>
            public static DateTime endCurrentWeek = startCurrentWeek.AddDays(7).AddSeconds(-1);   
    
            /// <summary>
            /// 上周周一
            /// </summary>
            public static DateTime startLastWeek = startCurrentWeek.AddDays(-7).Date;  
    
            /// <summary>
            /// 上周周日
            /// </summary>
            public static DateTime endLastWeek = startLastWeek.AddDays(7).AddSeconds(-1); 
    
            /// <summary>
            /// 本月月初
            /// </summary>
            public static DateTime startCurrentMonth = DateTime.Now.AddDays(1 - DateTime.Now.Day).Date; //本月月初  
    
            /// <summary>
            /// 本月月末
            /// </summary>
            public static DateTime endCurrentMonth = DateTime.Now.AddDays(1 - DateTime.Now.Day).Date.AddMonths(1).AddSeconds(-1); //本月月末  //DateTime endMonth = startMonth.AddDays((dt.AddMonths(1) - dt).Days - 1);  //本月月末    
    
            /// <summary>
            /// 上月月初
            /// </summary>
            public static DateTime startLastMonth = DateTime.Now.AddMonths(-1).Date.AddDays(1 - DateTime.Now.Day);
    
            /// <summary>
            /// 上月月末
            /// </summary>
            public static DateTime endLastMonth = DateTime.Parse(DateTime.Now.AddDays(1 - DateTime.Now.Day).ToShortDateString()).AddSeconds(-1);
    
            /// <summary>
            /// 本季度初
            /// </summary>
            public static DateTime startCurrentQuarter = DateTime.Now.AddMonths(0 - (DateTime.Now.Month - 1) % 3).AddDays(1 - DateTime.Now.Day).Date; //本季度初  
    
            /// <summary>
            /// 本季度末
            /// </summary>
            public static DateTime endCurrentQuarter = startCurrentQuarter.AddMonths(3).AddSeconds(-1); //本季度末   
    
            /// <summary>
            /// 上季度初
            /// </summary>
            public static DateTime startLastQuarter = startCurrentQuarter.AddMonths(-3).Date;
    
            /// <summary>
            /// 上季度末
            /// </summary>
            public static DateTime endLastQuarter = startLastQuarter.AddMonths(3).AddSeconds(-1);   
    
            /// <summary>
            /// 今年年初
            /// </summary>
            public static DateTime startCurrentYear = new DateTime(DateTime.Now.Year, 1, 1).Date; //本年年初 
    
            /// <summary>
            /// 今年年末
            /// </summary>
            public static DateTime endCurrentYear = new DateTime(DateTime.Now.Year, 12,31, 23, 59, 59); //本年年末
    
            /// <summary>
            /// 去年年初
            /// </summary>
            public static DateTime startLastYear = startCurrentYear.AddYears(-1).Date; 
    
            /// <summary>
            /// 去年年末
            /// </summary>
            public static DateTime endLastYear = endCurrentYear.AddYears(-1); 
    
            //至于昨天、明天、上周、上月、上季度、上年度等等,只要AddDays()、AddMonths()、AddYears()这几种方法组合一下就可以了。
    
        }
    

      

  • 相关阅读:
    WinCMD color命令
    WinCMD if命令(批处理Dos 大于、小于、等于)
    WinCMD 常用命令
    AIFramework基本概念整理
    AICompiler动态shape编译框架
    深度学习编译与优化Deep Learning Compiler and Optimizer
    NNVM Compiler,AI框架的开放式编译器
    Tengine AIFramework框架
    Tvm一些基本技术
    北汽蓝谷和北汽新能源
  • 原文地址:https://www.cnblogs.com/baozi789654/p/15810747.html
Copyright © 2020-2023  润新知