獲取UTC
/// <summary> /// 獲取時間戳 /// </summary> /// <returns>UTC</returns> public static string GetTimeStamp() { TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0); return Convert.ToInt64(ts.TotalSeconds).ToString(); }
轉換時間戳為C#時間
/// <summary> /// 轉換時間戳為C#時間 /// </summary> /// <param name="timeStamp">時間戳 單位:毫秒</param> /// <returns>C#時間</returns> public static DateTime ConvertTimeStampToDateTime(long timeStamp) { DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 當地時區 DateTime dt = startTime.AddSeconds(timeStamp); return dt; }
