增加时间转换为yyyyMMdd格式的扩展方法。

This commit is contained in:
falcon 2022-10-09 11:16:06 +08:00
parent 4eba08e692
commit dfb04f742c
2 changed files with 23 additions and 0 deletions

View File

@ -81,5 +81,15 @@ namespace Falcon.SugarApi.Test
Assert.IsTrue(r.Second == 0); Assert.IsTrue(r.Second == 0);
} }
/// <summary>
/// 时间格式转换测试
/// </summary>
[TestMethod]
public void DateToFormatTest() {
var d = new DateTime(2022, 1, 4, 2, 40, 25);
var s = d.ToyyyyMMdd();
Assert.IsTrue(s == "20220104");
}
} }
} }

View File

@ -9,6 +9,7 @@ namespace Falcon.SugarApi
/// </summary> /// </summary>
public static class DateTimeExtend public static class DateTimeExtend
{ {
#region
/// <summary> /// <summary>
/// 获取日期在某个时间段内的某一天 /// 获取日期在某个时间段内的某一天
/// </summary> /// </summary>
@ -78,6 +79,18 @@ namespace Falcon.SugarApi
public static Calendar GetDefaultCalendar(this DateTime dateTime) { public static Calendar GetDefaultCalendar(this DateTime dateTime) {
return CultureInfo.CurrentCulture.Calendar; return CultureInfo.CurrentCulture.Calendar;
} }
#endregion
#region
/// <summary>
/// 获取时间的yyyyMMdd表示形式
/// </summary>
/// <param name="date">时间</param>
/// <returns>yyyyMMdd格式时间</returns>
public static string ToyyyyMMdd(this DateTime date) => date.ToString("yyyyMMdd");
#endregion
} }
/// <summary> /// <summary>