C#獲取時間戳的方法


獲取時間戳的方法 

       /// <summary>
        /// 獲取時間戳
        /// </summary>
        /// <param name="nowTime">當前時間</param>
        /// <returns>時間戳(type:long)</returns>
        static long GetUnixTime(DateTime nowTime)
        {
            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1, 0, 0, 0,0));
            return (long)Math.Round((nowTime-startTime).TotalMilliseconds,MidpointRounding.AwayFromZero);
        }

 

把獲取到的時間戳轉換成byte數組:

byte [] timestamp = System.Text.Encoding.Default.GetBytes(GetUnixTime(DateTime.Now));

很簡單,但是還是寫下來吧,說不定以后能用上

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM