• 时间帮助类


    public class DateHelper
        {
            #region/// <summary>
            /// 获取时间所处周的第一天
            /// </summary>
            /// <param name="date"></param>
            /// <returns></returns>
            public static DateTime GetFirstDayOfWeek(DateTime date)
            {
                //星期一为第一天  
                var weekNow = Convert.ToInt32(date.DayOfWeek);
    
                //因为是以星期一为第一天,weekNow,要向前推6天。  
                weekNow = (weekNow == 0 ? (7 - 1) : (weekNow - 1));
                var dayDiff = (-1) * weekNow;
    
                //本周第一天  
                return date.AddDays(dayDiff);
            }
    
            /// <summary>
            /// 获取时间所处周的最后一天
            /// </summary>
            /// <param name="date"></param>
            /// <returns></returns>
            public static DateTime GetLastDayOfWeek(DateTime date)
            {
                //星期天为最后一天  
                var lastWeekDay = Convert.ToInt32(date.DayOfWeek);
                lastWeekDay = lastWeekDay == 0 ? (7 - lastWeekDay) : lastWeekDay;
                var lastWeekDiff = (7 - lastWeekDay);
    
                //本周最后一天  
               return date.AddDays(lastWeekDiff);
            }
    
            #endregion
    
            #region/// <summary>
            /// 获取时间所处月的第一天
            /// </summary>
            /// <param name="date"></param>
            /// <returns></returns>
            public static DateTime GetFirstDayOfMonth(DateTime date)
            {
                return date.AddDays(1 - date.Day);
            }
    
            /// <summary>
            /// 获取时间所处月的最后一天
            /// </summary>
            /// <param name="date"></param>
            /// <returns></returns>
            public static DateTime GetLastDayOfMonth(DateTime date)
            {
                return date.AddDays(1 - date.Day).AddMonths(1).AddDays(-1);
            }
    
            #endregion
    
            #region 季度
    
            /// <summary>
            /// 获取时间所处季度的第一天
            /// </summary>
            /// <param name="date"></param>
            /// <returns></returns>
            public static DateTime GetFirstDayOfQuarter(DateTime date)
            {
                return date.AddMonths(0 - (date.Month - 1) % 3).AddDays(1 - date.Day);
            }
    
            /// <summary>
            /// 获取时间所处季度的最后一天
            /// </summary>
            /// <param name="date"></param>
            /// <returns></returns>
            public static DateTime GetLastDayOfQuarter(DateTime date)
            {
                return date.AddMonths(0 - (date.Month - 1) % 3).AddDays(1 - date.Day).AddMonths(3).AddDays(-1);
            }
    
            #endregion
    
            #region 半年
    
            /// <summary>
            /// 获取所处时间半年的第一天(上半年或下半年)
            /// </summary>
            /// <param name="date"></param>
            /// <returns></returns>
            public static DateTime GetFirstDayOfHalfYear(DateTime date)
            {
                if (date.Date < new DateTime(date.Year, 7, 1))
                {
                    return new DateTime(date.Year, 1, 1);
                }
                return new DateTime(date.Year, 7, 1);
            }
    
            /// <summary>
            /// 获取所处时间半年的最后一天(上半年或下半年)
            /// </summary>
            /// <param name="date"></param>
            /// <returns></returns>
            public static DateTime GetLastDayOfHalfYear(DateTime date)
            {
                if (date.Date < new DateTime(date.Year, 7, 1))
                {
                    return new DateTime(date.Year, 7, 1).AddDays(-1);
                }
                return new DateTime(date.Year, 1, 1).AddYears(1).AddDays(-1);
            }
    
            #endregion
    
            #region/// <summary>
            /// 获取时间所处年的第一天
            /// </summary>
            /// <param name="date"></param>
            /// <returns></returns>
            public static DateTime GetFirstDayOfYear(DateTime date)
            {
                return new DateTime(date.Year, 1, 1);
            }
    
            /// <summary>
            /// 获取时间所处年的最后一天
            /// </summary>
            /// <param name="date"></param>
            /// <returns></returns>
            public static DateTime GetLastDayOfYear(DateTime date)
            {
                return new DateTime(date.Year, 1, 1).AddYears(1).AddDays(-1);
            }
    
            #endregion
    
            #region 相差天数
    
            /// <summary>
            /// 两日期之间相差天数
            /// </summary>
            /// <param name="starTime"></param>
            /// <param name="endTime"></param>
            /// <returns></returns>
            public static int DifferDays(DateTime? starTime, DateTime? endTime)
            {
                var result = 0;
                if (starTime.HasValue && endTime.HasValue)
                {
                    return endTime.Value.Subtract(starTime.Value).Days;
                }
                return result;
            }
            #endregion
    
            #region 相差周数
    
            public static int DifferWeeks(DateTime? starTime, DateTime? endTime)
            {
                int result = 0;
                if (starTime.HasValue && endTime.HasValue)
                {
                    var lastTime = GetLastDayOfWeek(starTime.Value);
                    while (lastTime < endTime)
                    {
                        result++;
                        lastTime = lastTime.AddDays(7);
                    }
                }
                return result;
            }
    
            #endregion
    
            #region 相差月数
    
            /// <summary>
            /// 相差月数
            /// </summary>
            /// <param name="starTime"></param>
            /// <param name="endTime"></param>
            /// <returns></returns>
            public static int DifferMonths(DateTime? starTime, DateTime? endTime)
            {
                var result = 0;
                if (starTime.HasValue && endTime.HasValue)
                {
                    return (endTime.Value.Year - starTime.Value.Year) * 12 +
                           (endTime.Value.Month - starTime.Value.Month);
                }
                return result;
            }
    
            #endregion
    
            #region 相差季度
    
            /// <summary>
            /// 相差季度
            /// </summary>
            /// <param name="starTime"></param>
            /// <param name="endTime"></param>
            /// <returns></returns>
            public static int DifferQuarters(DateTime? starTime, DateTime? endTime)
            {
                var result = 0;
                if (starTime.HasValue && endTime.HasValue)
                {
                    var lastTime = GetLastDayOfQuarter(starTime.Value);
                    while (lastTime < endTime)
                    {
                        result++;
                        lastTime = lastTime.AddMonths(3);
                    }
                }
                return result;
            }
    
            #endregion
    
            #region 相差半年
    
            /// <summary>
            /// 相差半年
            /// </summary>
            /// <param name="starTime"></param>
            /// <param name="endTime"></param>
            /// <returns></returns>
            public static int DifferHalfYears(DateTime? starTime, DateTime? endTime)
            {
                var result = 0;
                if (starTime.HasValue && endTime.HasValue)
                {
                    var lastTime = GetLastDayOfHalfYear(starTime.Value);
                    while (lastTime < endTime)
                    {
                        result++;
                        lastTime = lastTime.AddMonths(6);
                    }
                }
                return result;
            }
    
            #endregion
    
            #region 相差年数
    
            /// <summary>
            /// 相差年数
            /// </summary>
            /// <param name="starTime"></param>
            /// <param name="endTime"></param>
            /// <returns></returns>
            public static int DifferYears(DateTime? starTime, DateTime? endTime)
            {
                var result = 0;
                if (starTime.HasValue && endTime.HasValue)
                {
                    return endTime.Value.Year - starTime.Value.Year;
                }
                return result;
            }
    
            #endregion
    
            #region 获取两个日期之间的所有时间集合
    
            /// <summary>
            /// 获取两个日期之间的所有时间集合
            /// </summary>
            /// <param name="beginDate"></param>
            /// <param name="endDate"></param>
            /// <returns></returns>
            public  static List<string> GetDateTimeList(DateTime beginDate, DateTime endDate)
            {
                var result = new List<string>();
                var dateTime = beginDate;
                while (endDate >= dateTime)
                {
                    var item = dateTime.ToString("yy-MM-dd");
                    result.Add(item);
                    dateTime = dateTime.AddDays(1);
                }
    
                return result;
            }
    
            #endregion
        }
  • 相关阅读:
    服务器(Ubuntu 12.04 LTS)上编译基于OpenCV的项目遇到的问题及解决方案
    ubuntu 16.04 LTS 降级安装gcc 4.8
    C#程序中获取电脑硬件配置信息的一种方法
    C#程序将对象保存为json文件的方法
    C#中运用事件实现异步调用
    Redis实现分布式锁 php
    CI框架整合UEditor编辑器上传功能
    PHP给图片加水印具体实现
    检测网站是否被和谐!
    vue的双向绑定和依赖收集
  • 原文地址:https://www.cnblogs.com/xiaoxiaomini/p/14434929.html
Copyright © 2020-2023  润新知