C# DateTime类型和时间戳 互相转换
/// <summary>
/// 时间戳转为C#格式时间
/// </summary>
/// <param name=”timeStamp”></param>
/// <returns></returns>
public static DateTime GetTime(string timeStamp)
{
long lTime = long.Parse(timeStamp);
DateTime ndt = new DateTime(1970, 1, 1).ToUniversalTime();
return ndt.AddMilliseconds(lTime).AddHours(8);
}
/// <summary>
/// DateTime时间格式转换为Unix时间戳格式
/// </summary>
/// <param name=”time”></param>
/// <returns></returns>
public static long ConvertDateTimeInt(System.DateTime time)
{
DateTime dt = time.ToUniversalTime();
DateTime st = new DateTime(1970, 1, 1).ToUniversalTime();
return (long)(dt - st).TotalMilliseconds;
}