C#获取时间戳的方法

时间:2023-03-10 02:19:32
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));

很简单,但是还是写下来吧,说不定以后能用上