unix時間轉換為datetimedatetime轉換為unixtime

BIGBOY_GU發表於2018-07-06

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

相關文章