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.
59 lines
1.6 KiB
C#
59 lines
1.6 KiB
C#
using Newtonsoft.Json.Linq;
|
|
|
|
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];
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取datetime值
|
|
/// </summary>
|
|
/// <param name="jobj"></param>
|
|
/// <param name="prop"></param>
|
|
/// <returns></returns>
|
|
public static DateTime? GetDateTimeValue(this JObject jobj, string prop)
|
|
{
|
|
var jt = jobj[prop];
|
|
if (jt == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var strVal = jt.ToString();
|
|
DateTime rtnVal = DateTime.MinValue;
|
|
if (DateTime.TryParse(strVal, out rtnVal))
|
|
{
|
|
return rtnVal;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
} |