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.

73 lines
2.4 KiB
C#

using System;
namespace Admin.Core.Common
{
public class DateHelper
{
public static DateTime StampToDateTime(string time)
{
time = time.Substring(0, 10);
double timestamp = Convert.ToInt64(time);
System.DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
dateTime = dateTime.AddSeconds(timestamp).ToLocalTime();
return dateTime;
}
public static string TimeSubTract(DateTime time1, DateTime time2)
{
TimeSpan subTract = time1.Subtract(time2);
return $"{subTract.Days} 天 {subTract.Hours} 时 {subTract.Minutes} 分 ";
}
/// <summary>
/// 时间戳转本地时间-时间戳精确到秒
/// </summary>
public static DateTime ToLocalTimeDateBySeconds(long unix)
{
var dto = DateTimeOffset.FromUnixTimeSeconds(unix);
return dto.ToLocalTime().DateTime;
}
/// <summary>
/// 时间转时间戳Unix-时间戳精确到秒
/// </summary>
public static long ToUnixTimestampBySeconds(DateTime dt)
{
DateTimeOffset dto = new DateTimeOffset(dt);
return dto.ToUnixTimeSeconds();
}
/// <summary>
/// 时间戳转本地时间-时间戳精确到毫秒
/// </summary>
public static DateTime ToLocalTimeDateByMilliseconds(long unix)
{
var dto = DateTimeOffset.FromUnixTimeMilliseconds(unix);
return dto.ToLocalTime().DateTime;
}
/// <summary>
/// 时间转时间戳Unix-时间戳精确到毫秒
/// </summary>
public static long ToUnixTimestampByMilliseconds(DateTime dt)
{
DateTimeOffset dto = new DateTimeOffset(dt);
return dto.ToUnixTimeMilliseconds();
}
/// <summary>
/// 时间差天数
/// </summary>
/// <param name="dateStart"></param>
/// <param name="dateEnd"></param>
/// <returns></returns>
public static int DateDiff(DateTime dateStart, DateTime dateEnd)
{
DateTime start = Convert.ToDateTime(dateStart.ToShortDateString());
DateTime end = Convert.ToDateTime(dateEnd.ToShortDateString());
TimeSpan sp = end.Subtract(start);
return sp.Days;
}
}
}