C# - Timestamp 與 DateTime 互轉

C# - Timestamp 與 DateTime 互轉

  1. // seconds since 1970-01-01 00:00:00 UTC
  2. // 現在時間轉秒數
  3. double timestamp = (DateTime.Now.AddHours ( -8 ) - new DateTime( 1970, 1, 1, 0, 0, 0 ) ).TotalSeconds;
  4. // 秒數轉 DateTime
  5. DateTime dt = ( new DateTime( 1970, 1, 1, 0, 0, 0 ) ).AddHours ( 8 ).AddSeconds (timestamp);
  6. MessageBox.Show (dt.ToString ( ) );