初始提交
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Hncore.Infrastructure.Common
|
||||
{
|
||||
public class DateTimeHelper
|
||||
{
|
||||
public static DateTime SqlMinTime => Convert.ToDateTime("1975-01-01 00:00:00");
|
||||
|
||||
public static DateTime SqlMaxTime => Convert.ToDateTime("9999-12-31 23:59:59");
|
||||
|
||||
/// <summary>
|
||||
/// 将10位时间戳转时间
|
||||
/// </summary>
|
||||
/// <param name="unixTimeStamp"></param>
|
||||
/// <returns></returns>
|
||||
public static DateTime UnixTimeStampToDateTime(long unixTimeStamp)
|
||||
{
|
||||
// Unix timestamp is seconds past epoch
|
||||
DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
|
||||
dtDateTime = dtDateTime.AddSeconds(unixTimeStamp).ToLocalTime();
|
||||
return dtDateTime;
|
||||
}
|
||||
|
||||
public static long ToUnixTimestamp(DateTime target)
|
||||
{
|
||||
return Convert.ToInt64((TimeZoneInfo.ConvertTimeToUtc(target) -
|
||||
new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将13位时间戳转为时间
|
||||
/// </summary>
|
||||
/// <param name="javaTimeStamp"></param>
|
||||
/// <returns></returns>
|
||||
public static DateTime JsTimeStampToDateTime(double javaTimeStamp)
|
||||
{
|
||||
var dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
|
||||
dtDateTime = dtDateTime.AddMilliseconds(javaTimeStamp).ToLocalTime();
|
||||
return dtDateTime;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取时间戳
|
||||
/// </summary>
|
||||
/// <param name="target"></param>
|
||||
/// <param name="bflag">为真时获取10位时间戳,为假时获取13位时间戳</param>
|
||||
/// <returns></returns>
|
||||
public static long ToUnixTime(DateTime target, bool bflag = false)
|
||||
{
|
||||
TimeSpan ts = target - new DateTime(1970, 1, 1, 0, 0, 0, 0);
|
||||
long timer = 0;
|
||||
timer = Convert.ToInt64(!bflag ? ts.TotalSeconds : ts.TotalMilliseconds);
|
||||
return timer;
|
||||
}
|
||||
|
||||
public static TimeZoneInfo GetCstTimeZoneInfo()
|
||||
{
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
||||
{
|
||||
return TimeZoneInfo.FindSystemTimeZoneById("Asia/Shanghai");
|
||||
}
|
||||
|
||||
return TimeZoneInfo.FindSystemTimeZoneById("China Standard Time");
|
||||
}
|
||||
|
||||
public static bool IsSameDayOrLess(DateTime dt1, DateTime dt2)
|
||||
{
|
||||
return dt1.Year == dt2.Year
|
||||
&& dt1.Month == dt2.Month
|
||||
&& dt1.Day == dt2.Day
|
||||
|| dt1 < dt2;
|
||||
}
|
||||
|
||||
public static DateTime GetDatePart(DateTime dt)
|
||||
{
|
||||
return new DateTime(dt.Year, dt.Month, dt.Day);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user