/// <summary>
/// 时间戳和日期的转化
/// </summary>
public class DateTimeUtility
{
/// <summary>
/// 日期转换为时间戳(时间戳单位秒)
/// </summary>
/// <param name="TimeStamp"></param>
/// <returns></returns>
public static long ConvertToTimeStamp(DateTime time)
{
DateTime Jan1st1970 = new DateTime (, , , , , , DateTimeKind.Utc);
return (long)(time.AddHours(-) - Jan1st1970).TotalMilliseconds;
} /// <summary>
/// 时间戳转换为日期(时间戳单位秒)
/// </summary>
/// <param name="TimeStamp"></param>
/// <returns></returns>
public static DateTime ConvertToDateTime(long timeStamp)
{
var start = new DateTime(, , , , , , DateTimeKind.Utc);
return start.AddMilliseconds(timeStamp).AddHours();
} }
如果是精确到秒级别的
把Milliseconds换成seconds即可。