1970年1月1日(00:00:00 GMT)Unix 时间戳(Unix Timestamp)对时间转换
将Long类型转换为DateTime类型,时间戳为毫秒的转换
/// <summary>
/// 将Long类型转换为DateTime类型
/// </summary>
/// <param name="d">long</param>
/// <returns></returns>
public static DateTime ConvertLongDateTime(long ticks)
{
DateTime dtBase = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
DateTime convertTime = dtBase.Add(new TimeSpan(ticks * TimeSpan.TicksPerMillisecond)).ToLocalTime();
return convertTime;
}
时间戳以秒为单位的转换
/// <summary>
/// 将Long类型转换为DateTime类型
/// </summary>
/// <param name="d">long</param>
/// <returns></returns>
public static DateTime ConvertLongDateTime(long ticks)
{
DateTime dtBase = new DateTime(, , , , , , DateTimeKind.Utc);
DateTime convertTime = dtBase.Add(new TimeSpan(ticks * TimeSpan.TicksPerSecond)).ToLocalTime();
return convertTime;
}
将DateTime类型转换为long类型
/// <summary>
/// 将DateTime类型转换为long类型
/// </summary>
/// <param name="dt">时间</param>
/// <returns></returns>
public static long ConvertDataTimeLong(DateTime dt)
{
DateTime dtBase = new DateTime(, , , , , , DateTimeKind.Utc);
TimeSpan toNow = dt.ToUniversalTime().Subtract(dtBase);
long timeStamp = toNow.Ticks/;
return timeStamp;
}
用C#转出的结果如果是new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc),因为精度问题需要/10000 获取以毫秒精度的时间戳 /10000000获取以秒为精度的时间戳