其他分享
首页 > 其他分享> > 根据时间段枚举类型转换时间

根据时间段枚举类型转换时间

作者:互联网

所需枚举

 /// <summary>
    /// 时间范围
    /// </summary>
    public enum DateRangeEnum
    {
        /// <summary>
        /// 全部
        /// </summary>
        [Description("全部")]
        All = 0,
        /// <summary>
        /// 今天
        /// </summary>
        [Description("今天")]
        Today = 1,
        /// <summary>
        /// 明天
        /// </summary>
        [Description("明天")]
        Tomorrow = 2,
        /// <summary>
        /// 昨天
        /// </summary>
        [Description("昨天")]
        Yesterday = 3,

        /// <summary>
        /// 本周
        /// </summary>
        [Description("本周")]
        ThisWeek = 6,
        /// <summary>
        /// 下周
        /// </summary>
        [Description("下周")]
        NextWeek = 7,
        /// <summary>
        /// 上周
        /// </summary>
        [Description("上周")]
        LastWeek = 8,
        /// <summary>
        /// 本月
        /// </summary>
        [Description("本月")]
        ThisMonth = 10,
        /// <summary>
        /// 上月
        /// </summary>
        [Description("上月")]
        LastMonth = 11,
        /// <summary>
        /// 下月
        /// </summary>
        [Description("下月")]
        NextMonth = 12,
        /// <summary>
        /// 一月以内
        /// </summary>
        [Description("一月以内")]
        OneMonth = 13,
        /// <summary>
        /// 半年以内
        /// </summary>
        [Description("半年以内")]
        HalfYear = 14,
        /// <summary>
        /// 一年以内
        /// </summary>
        [Description("一年以内")]
        OneYear = 15,
        /// <summary>
        /// 本年
        /// </summary>
        [Description("本年")]
        ThisYear = 16,
        /// <summary>
        /// 去年
        /// </summary>
        [Description("去年")]
        LastYear = 17
    }

功能实现

/// <summary>
            /// 根据时间段枚举类型转换时间
            /// </summary>
            /// <param name="type"></param>
            /// <returns></returns>
            public static (DateTime, DateTime) FormatDateRange(DateRangeEnum type)
            {
                string startDate = string.Empty, endDate = string.Empty;
                DateTime now = DateTime.Now;
                DateTime? start = null;
                switch (type)
                {
                    case DateRangeEnum.OneMonth:
                        start = now.AddMonths(-1);
                        startDate = $"{start.Value.ToString("yyyy-MM-dd")} 00:00";
                        endDate = $"{now.ToString("yyyy-MM-dd")} 23:59:59";
                        break;
                    case DateRangeEnum.HalfYear:
                        start = now.AddMonths(-6);
                        startDate = $"{start.Value.ToString("yyyy-MM-dd")} 00:00";
                        endDate = $"{now.ToString("yyyy-MM-dd")} 23:59:59";
                        break;
                    case DateRangeEnum.OneYear:
                        start = now.AddYears(-1);
                        startDate = $"{start.Value.ToString("yyyy-MM-dd")} 00:00";
                        endDate = $"{now.ToString("yyyy-MM-dd")} 23:59:59";
                        break;


                    case DateRangeEnum.Today:
                        start = now;
                        startDate = $"{start.Value.ToString("yyyy-MM-dd")} 00:00";
                        endDate = $"{now.ToString("yyyy-MM-dd")} 23:59:59";
                        break;
                    case DateRangeEnum.Tomorrow:
                        startDate = $"{now.AddDays(1).ToString("yyyy-MM-dd")} 00:00";
                        endDate = $"{now.AddDays(1).ToString("yyyy-MM-dd")} 23:59:59";
                        break;
                    case DateRangeEnum.Yesterday:
                        start = now.AddDays(-1);
                        startDate = $"{start.Value.ToString("yyyy-MM-dd")} 00:00";
                        endDate = $"{start.Value.ToString("yyyy-MM-dd")} 23:59:59";
                        break;

                    case DateRangeEnum.NextWeek:
                        start = now.AddDays(0 - Convert.ToInt16(DateTime.Now.DayOfWeek) + 8);
                        startDate = $"{start.Value.ToString("yyyy-MM-dd")} 00:00";
                        start = now.AddDays(6 - Convert.ToInt16(DateTime.Now.DayOfWeek) + 8);
                        endDate = $"{start.Value.ToString("yyyy-MM-dd")} 23:59:59";
                        break;
                    case DateRangeEnum.ThisWeek:
                        start = now.AddDays(0 - Convert.ToInt16(DateTime.Now.DayOfWeek) + 1);
                        startDate = $"{start.Value.ToString("yyyy-MM-dd")} 00:00";
                        start = now.AddDays(6 - Convert.ToInt16(DateTime.Now.DayOfWeek) + 1);
                        endDate = $"{start.Value.ToString("yyyy-MM-dd")} 23:59:59";
                        break;
                    case DateRangeEnum.LastWeek:
                        start = now.AddDays(0 - Convert.ToInt16(DateTime.Now.DayOfWeek) - 6);
                        startDate = $"{start.Value.ToString("yyyy-MM-dd")} 00:00";
                        start = now.AddDays(6 - Convert.ToInt16(DateTime.Now.DayOfWeek) - 6);
                        endDate = $"{start.Value.ToString("yyyy-MM-dd")} 23:59:59";
                        break;

                    case DateRangeEnum.ThisMonth:
                        start = new DateTime(now.Year, now.Month, 1);
                        startDate = $"{start.Value.ToString("yyyy-MM-dd")} 00:00";
                        endDate = $"{start.Value.AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd")} 23:59:59";
                        break;
                    case DateRangeEnum.LastMonth:
                        start = new DateTime(now.Year, now.Month, 1).AddMonths(-1);
                        startDate = $"{start.Value.ToString("yyyy-MM-dd")} 00:00";
                        endDate = $"{start.Value.AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd")} 23:59:59";
                        break;
                    case DateRangeEnum.NextMonth:
                        start = new DateTime(now.Year, now.Month, 1).AddMonths(1);
                        startDate = $"{start.Value.ToString("yyyy-MM-dd")} 00:00";
                        endDate = $"{start.Value.AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd")} 23:59:59";
                        break;
                    case DateRangeEnum.ThisYear:
                        start = new DateTime(now.Year, 1, 1);
                        startDate = $"{start.Value.ToString("yyyy-MM-dd")} 00:00";
                        endDate = $"{start.Value.AddYears(1).AddDays(-1).ToString("yyyy-MM-dd")} 23:59:59";
                        break;
                    case DateRangeEnum.LastYear:
                        start = new DateTime(now.Year - 1, 1, 1);
                        startDate = $"{start.Value.ToString("yyyy-MM-dd")} 00:00";
                        endDate = $"{start.Value.AddYears(1).AddDays(-1).ToString("yyyy-MM-dd")} 23:59:59";
                        break;


                }
                return (BaseHelper.ToDateTime(startDate, DateTime.Now), BaseHelper.ToDateTime(endDate, DateTime.Now));
            }
        }

 

标签:类型转换,00,59,MM,dd,start,枚举,时间段,ToString
来源: https://www.cnblogs.com/Stars0121/p/16529118.html