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