TimeHelp 获取时间戳转换类

时间:2022-12-06 20:40:45
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Share
{
public class TimeHelp
{
/// <summary>
/// 获取时间戳 10位
/// </summary>
/// <returns></returns>
public static string GetTimeStamp()
{
TimeSpan ts = DateTime.UtcNow - new DateTime(, , , , , , );
return Convert.ToInt64(ts.TotalSeconds).ToString();
} /// <summary>
/// 获取时间戳 13
/// </summary>
/// <returns></returns>
public static string GetTimeStamp(System.DateTime time)
{
long ts = ConvertDateTimeToInt(time);
return ts.ToString();
}
/// <summary>
/// 将c# DateTime时间格式转换为Unix时间戳格式
/// </summary>
/// <param name="time">时间</param>
/// <returns>long</returns>
public static long ConvertDateTimeToInt(System.DateTime time)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(, , , , , , ));
long t = (time.Ticks - startTime.Ticks) / ; //除10000调整为13位
return t;
}
/// <summary>
/// 时间戳转为C#格式时间
/// </summary>
/// <param name=”timeStamp”></param>
/// <returns></returns>
public static DateTime ConvertStringToDateTime(string timeStamp)
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(, , ));
long lTime = long.Parse(timeStamp + "");
TimeSpan toNow = new TimeSpan(lTime);
return dtStart.Add(toNow);
}
}
}