增加日期扩展方法。

This commit is contained in:
falcon 2022-09-15 12:32:08 +08:00
parent b30592b6ae
commit 82745b15fc
2 changed files with 227 additions and 0 deletions

View File

@ -0,0 +1,85 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
namespace Falcon.SugarApi.Test
{
/// <summary>
/// 日期时间扩展方法测试
/// </summary>
[TestClass]
public class DateTimeExtendTest
{
/// <summary>
/// 测试过程
/// </summary>
[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);
}
}
}

View File

@ -0,0 +1,142 @@
using Microsoft.Extensions.Options;
using System;
using System.Globalization;
namespace Falcon.SugarApi
{
/// <summary>
/// 时间类型扩展
/// </summary>
public static class DateTimeExtend
{
/// <summary>
/// 获取日期在某个时间段内的某一天
/// </summary>
/// <param name="date">日期</param>
/// <param name="option">查询选项</param>
/// <returns>符合条件日期</returns>
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("不支持这个日期查询");
}
/// <summary>
/// 返回本周的第一天的日期。默认星期一为第一天
/// </summary>
/// <param name="date">本周的一天</param>
/// <param name="firstDayOfWeek">默认第一天是星期几</param>
/// <returns>本周第一天</returns>
public static DateTime GetFirstDayOfTheWeek(this DateTime date, DayOfWeek firstDayOfWeek = DayOfWeek.Monday) {
return date.GetDay(new DateOption { DateRangeType = DateRangeType.Weekly, DateType = DateType.FirstDay, DayOfWeek = firstDayOfWeek, });
}
/// <summary>
/// 获取一个月中的第一天
/// </summary>
/// <param name="date">日期</param>
/// <returns>日期</returns>
public static DateTime GetFirstDayOfTheMonth(this DateTime date) {
return date.GetDay(new DateOption { DateRangeType = DateRangeType.Monthly, DateType = DateType.FirstDay });
}
/// <summary>
/// 获取一年中的第一天
/// </summary>
/// <param name="date">日期</param>
/// <returns>日期</returns>
public static DateTime GetFirstDayOfTheYear(this DateTime date) {
return date.GetDay(new DateOption { DateRangeType = DateRangeType.Yearly, DateType = DateType.FirstDay });
}
/// <summary>
/// 获取日期为一年中的第几周
/// </summary>
/// <param name="date">日期</param>
/// <param name="firstDayOfWeek">第一天是星期几</param>
/// <returns>第几周</returns>
public static int GetWeekOfTheYear(this DateTime date, DayOfWeek firstDayOfWeek = DayOfWeek.Monday) {
return date.GetDefaultCalendar().GetWeekOfYear(date, CalendarWeekRule.FirstDay, firstDayOfWeek);
}
/// <summary>
/// 获取默认的当前线程区域设置的日历
/// </summary>
/// <param name="dateTime">无关紧要,可以是任何时间</param>
/// <returns></returns>
public static Calendar GetDefaultCalendar(this DateTime dateTime) {
return CultureInfo.CurrentCulture.Calendar;
}
}
/// <summary>
/// 日期操作选项
/// </summary>
public class DateOption : IOptions<DateOption>
{
/// <summary>
/// 日期类型,默认第一天
/// </summary>
public DateType DateType { get; set; } = DateType.FirstDay;
/// <summary>
/// 日期范围类型,默认每月
/// </summary>
public DateRangeType DateRangeType { get; set; } = DateRangeType.Monthly;
/// <summary>
/// 哪一天是一个星期的第一天,默认星期一
/// </summary>
public DayOfWeek DayOfWeek { get; set; } = DayOfWeek.Monday;
/// <summary>
/// 保持时间不变默认false
/// </summary>
public bool RetentionTime { get; set; } = false;
/// <summary>
/// 默认每个月第一天
/// </summary>
public DateOption Value => this;
}
/// <summary>
/// 日期类型
/// </summary>
public enum DateType
{
/// <summary>
/// 第一天
/// </summary>
FirstDay,
/// <summary>
/// 最后一天
/// </summary>
LastDay,
}
/// <summary>
/// 日期范围类型
/// </summary>
public enum DateRangeType
{
/// <summary>
/// 每周
/// </summary>
Weekly,
/// <summary>
/// 每月
/// </summary>
Monthly,
/// <summary>
/// 每年
/// </summary>
Yearly,
}
}