You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 lines
1.0 KiB
C#

namespace DS.Module.Core.Extensions;
public static class DateTimeExtension
{
/// <summary>
/// 时间戳起始日期
/// </summary>
public static readonly DateTime TimestampStart = new(1970, 1, 1, 0, 0, 0, 0);
/// <summary>
/// 转换为时间戳
/// </summary>
/// <param name="dateTime"></param>
/// <param name="milliseconds">是否使用毫秒</param>
/// <returns></returns>
public static long ToTimestamp(this DateTime dateTime, bool milliseconds = false)
{
var timestamp = dateTime.ToUniversalTime() - TimestampStart;
return (long)(milliseconds ? timestamp.TotalMilliseconds : timestamp.TotalSeconds);
}
/// <summary>
/// 获取周几
/// </summary>
/// <param name="datetime"></param>
/// <returns></returns>
public static string GetWeekName(this DateTime datetime)
{
var day = (int)datetime.DayOfWeek;
var week = new string[] { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
return week[day];
}
}