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;
}

posted @ 2015-12-23 16:25  乔安生  阅读(1205)  评论(0编辑  收藏  举报