C# 获取当前时间戳,将时间戳转换为当前本地时间

时间:2022-03-14 02:35:10
/// <summary>
/// 获取当前本地时间戳
/// </summary>
/// <returns></returns>
public long GetCurrentTimeUnix()
{
TimeSpan cha = (DateTime.Now - TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)));
long t = (long)cha.TotalSeconds;
return t;
}
/// <summary>
/// 时间戳转换为本地时间对象
/// </summary>
/// <returns></returns>
public DateTime GetUnixDateTime(long unix)
{
//long unix = 1500863191;
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
DateTime newTime= dtStart.AddSeconds(unix);
return newTime;
}