C#獲取時(shí)間戳

時(shí)間戳轉(zhuǎn)為C#格式時(shí)間

        /// <summary>
        /// 獲取時(shí)間戳
        /// </summary>
        /// <returns></returns>
        public static string GetTimeStamp()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalSeconds).ToString();
        }
        /// <summary>
        /// 時(shí)間戳轉(zhuǎn)為C#格式時(shí)間
        /// </summary>
        /// <param name="timeStamp">Unix時(shí)間戳格式</param>
        /// <returns>C#格式時(shí)間</returns>
        public static DateTime GetStampDateTime(long timeStamp)
        {
            DateTime time = new DateTime();
            try
            {
                DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
                long lTime = long.Parse(timeStamp   "0000000");
                TimeSpan toNow = new TimeSpan(lTime);
                time = dtStart.Add(toNow);
            }
            catch
            {
                time = DateTime.Now.AddDays(-30);
            }
            return time;
        }


原文鏈接:C#獲取時(shí)間戳,時(shí)間戳轉(zhuǎn)為C#格式時(shí)間