unix时间转换为datetime\datetime转换为unixtime

/// <summary>
/// unix时间转换为datetime
/// </summary>
/// <param name="timeStamp"></param>
/// <returns></returns>
public static DateTime UnixTimeToTime(string timeStamp)
{
  DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
  long lTime = long.Parse(timeStamp + "0000000");
  TimeSpan toNow = new TimeSpan(lTime);
  return dtStart.Add(toNow);
}

 

/// <summary>
/// datetime转换为unixtime
/// </summary>
/// <param name="time"></param>
/// <returns></returns>
public static int ConvertDateTimeInt(System.DateTime time)
{
  System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
  return (int)(time - startTime).TotalSeconds;
}

posted @ 2018-07-06 15:39  GU天乐乐乐!  阅读(2617)  评论(3编辑  收藏  举报