diff --git a/Falcon.SugarApi.Test/DateTimeExtendTest.cs b/Falcon.SugarApi.Test/DateTimeExtendTest.cs new file mode 100644 index 0000000..baa30b9 --- /dev/null +++ b/Falcon.SugarApi.Test/DateTimeExtendTest.cs @@ -0,0 +1,85 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; + +namespace Falcon.SugarApi.Test +{ + /// + /// 日期时间扩展方法测试 + /// + [TestClass] + public class DateTimeExtendTest + { + /// + /// 测试过程 + /// + [TestMethod] + public void TestFunction() { + var dt = DateTime.Now; + Console.WriteLine($"当前时间:{dt:yyyy-MM-dd}"); + DateTime r; + int y = dt.Year, m = dt.Month, d = dt.Day; + + r = dt.GetFirstDayOfTheWeek(); + Console.WriteLine($"本周第一天(默认礼拜一):{r:yyyy-MM-dd}"); + Assert.IsTrue(r.DayOfWeek == DayOfWeek.Monday, "默认星期一为第一天"); + Assert.IsTrue(r.Year == y); + Assert.IsTrue(r.Month == m); + Assert.IsTrue(r.Hour == 0); + Assert.IsTrue(r.Minute == 0); + Assert.IsTrue(r.Second == 0); + + r = dt.GetFirstDayOfTheWeek(DayOfWeek.Sunday); + Console.WriteLine($"本周第一天(礼拜天):{r:yyyy-MM-dd}"); + Assert.IsTrue(r.DayOfWeek == DayOfWeek.Sunday, "默认星期天为第一天"); + Assert.IsTrue(r.Year == y); + Assert.IsTrue(r.Month == m); + + r = new DateTime(2022, 1, 1); + Assert.IsTrue(r.GetWeekOfTheYear() == 1); + r = new DateTime(2022, 1, 5); + Assert.IsTrue(r.GetWeekOfTheYear() == 2); + Assert.IsTrue(r.Hour == 0); + Assert.IsTrue(r.Minute == 0); + Assert.IsTrue(r.Second == 0); + + r = dt.GetFirstDayOfTheYear(); + Console.WriteLine($"当年的第一天{r:yyyy-MM-dd}"); + Assert.IsTrue(r.Year == dt.Year); + Assert.IsTrue(r.Month == 1); + Assert.IsTrue(r.Day == 1); + Assert.IsTrue(r.Hour == 0); + Assert.IsTrue(r.Minute == 0); + Assert.IsTrue(r.Second == 0); + + r = dt.GetDay(new DateOption { + DateRangeType = DateRangeType.Monthly, DateType = DateType.FirstDay, RetentionTime = true, + }); + Console.WriteLine($"当月的第一天保持时间{r:yyyy-MM-dd HH:mm:ss}"); + Assert.IsTrue(r.Year == dt.Year); + Assert.IsTrue(r.Month == dt.Month); + Assert.IsTrue(r.Day == 1); + Assert.IsTrue(r.Hour == dt.Hour); + Assert.IsTrue(r.Minute == dt.Minute); + Assert.IsTrue(r.Second == dt.Second); + + r = dt.GetFirstDayOfTheMonth(); + Console.WriteLine($"当月的第一天{r:yyyy-MM-dd HH:mm:ss}"); + Assert.IsTrue(r.Year == dt.Year); + Assert.IsTrue(r.Month == dt.Month); + Assert.IsTrue(r.Day == 1); + Assert.IsTrue(r.Hour == 0); + Assert.IsTrue(r.Minute == 0); + Assert.IsTrue(r.Second == 0); + + r = dt.GetDay(new DateOption { DateType = DateType.LastDay }); + Console.WriteLine($"当月的最后一天{r:yyyy-MM-dd HH:mm:ss}"); + Assert.IsTrue(r.Year == dt.Year); + Assert.IsTrue(r.Month == dt.Month); + Assert.IsTrue(r.Day == 29 || r.Day == 30 || r.Day == 31 ); + Assert.IsTrue(r.Hour == 0); + Assert.IsTrue(r.Minute == 0); + Assert.IsTrue(r.Second == 0); + + } + } +} diff --git a/Falcon.SugarApi/DateTimeExtend.cs b/Falcon.SugarApi/DateTimeExtend.cs new file mode 100644 index 0000000..7ad4604 --- /dev/null +++ b/Falcon.SugarApi/DateTimeExtend.cs @@ -0,0 +1,142 @@ +using Microsoft.Extensions.Options; +using System; +using System.Globalization; + +namespace Falcon.SugarApi +{ + /// + /// 时间类型扩展 + /// + public static class DateTimeExtend + { + /// + /// 获取日期在某个时间段内的某一天 + /// + /// 日期 + /// 查询选项 + /// 符合条件日期 + public static DateTime GetDay(this DateTime date, DateOption option) { + date = option.RetentionTime ? date : new DateTime(date.Year, date.Month, date.Day, 0, 0, 0); + var drt = option.DateRangeType; + var dt = option.DateType; + var firstDayOfWeek = option.DayOfWeek; + return + drt == DateRangeType.Weekly && dt == DateType.FirstDay ? date.AddDays(firstDayOfWeek - date.DayOfWeek) : + drt == DateRangeType.Weekly && dt == DateType.LastDay ? date.AddDays(firstDayOfWeek - date.DayOfWeek + 7) : + drt == DateRangeType.Monthly && dt == DateType.FirstDay ? date.AddDays(1 - date.Day) : + drt == DateRangeType.Monthly && dt == DateType.LastDay ? date.AddDays(1 - date.Day).AddMonths(1).AddDays(-1) : + drt == DateRangeType.Yearly && dt == DateType.FirstDay ? new DateTime(date.Year, 1, 1) : + drt == DateRangeType.Yearly && dt == DateType.LastDay ? new DateTime(date.Year, 1, 1).AddYears(1).AddDays(-1) : + + throw new NotSupportedException("不支持这个日期查询"); + } + + + /// + /// 返回本周的第一天的日期。默认星期一为第一天 + /// + /// 本周的一天 + /// 默认第一天是星期几 + /// 本周第一天 + public static DateTime GetFirstDayOfTheWeek(this DateTime date, DayOfWeek firstDayOfWeek = DayOfWeek.Monday) { + return date.GetDay(new DateOption { DateRangeType = DateRangeType.Weekly, DateType = DateType.FirstDay, DayOfWeek = firstDayOfWeek, }); + } + + /// + /// 获取一个月中的第一天 + /// + /// 日期 + /// 日期 + public static DateTime GetFirstDayOfTheMonth(this DateTime date) { + return date.GetDay(new DateOption { DateRangeType = DateRangeType.Monthly, DateType = DateType.FirstDay }); + } + + /// + /// 获取一年中的第一天 + /// + /// 日期 + /// 日期 + public static DateTime GetFirstDayOfTheYear(this DateTime date) { + return date.GetDay(new DateOption { DateRangeType = DateRangeType.Yearly, DateType = DateType.FirstDay }); + } + + /// + /// 获取日期为一年中的第几周 + /// + /// 日期 + /// 第一天是星期几 + /// 第几周 + public static int GetWeekOfTheYear(this DateTime date, DayOfWeek firstDayOfWeek = DayOfWeek.Monday) { + return date.GetDefaultCalendar().GetWeekOfYear(date, CalendarWeekRule.FirstDay, firstDayOfWeek); + } + + /// + /// 获取默认的当前线程区域设置的日历 + /// + /// 无关紧要,可以是任何时间 + /// + public static Calendar GetDefaultCalendar(this DateTime dateTime) { + return CultureInfo.CurrentCulture.Calendar; + } + } + + /// + /// 日期操作选项 + /// + public class DateOption : IOptions + { + /// + /// 日期类型,默认第一天 + /// + public DateType DateType { get; set; } = DateType.FirstDay; + /// + /// 日期范围类型,默认每月 + /// + public DateRangeType DateRangeType { get; set; } = DateRangeType.Monthly; + /// + /// 哪一天是一个星期的第一天,默认星期一 + /// + public DayOfWeek DayOfWeek { get; set; } = DayOfWeek.Monday; + /// + /// 保持时间不变,默认false + /// + public bool RetentionTime { get; set; } = false; + + /// + /// 默认每个月第一天 + /// + public DateOption Value => this; + } + /// + /// 日期类型 + /// + public enum DateType + { + /// + /// 第一天 + /// + FirstDay, + /// + /// 最后一天 + /// + LastDay, + } + /// + /// 日期范围类型 + /// + public enum DateRangeType + { + /// + /// 每周 + /// + Weekly, + /// + /// 每月 + /// + Monthly, + /// + /// 每年 + /// + Yearly, + } +}