C#时间戳的获取与转换方法

时间:2023-12-13 21:51:14
/// <summary>
/// 获取当前时间戳
/// </summary>
/// <param name="bflag"></param>
/// <returns></returns>
public static string GetTimeStamp()
{
return GetTimeStamp(DateTime.Now);
}
/// <summary>
/// 获取指定时间戳
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static string GetTimeStamp(DateTime date)
{
TimeSpan ts = date - new DateTime(, , , , , , );
return Convert.ToInt64(ts.TotalSeconds).ToString(); ;
}
/// <summary>
/// 将时间戳转换成DateTime
/// </summary>
/// <param name="timeStamp"></param>
/// <returns></returns>
private DateTime GetTime(string timeStamp)
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(, , ));
long lTime = long.Parse(timeStamp + "");
TimeSpan toNow = new TimeSpan(lTime); return dtStart.Add(toNow);
}