1. <wbr id="m8vu6"></wbr>

      <del id="m8vu6"><center id="m8vu6"><source id="m8vu6"></source></center></del>
        <p id="m8vu6"><sub id="m8vu6"></sub></p>

        VB.net 2010 視頻教程 VB.net 2010 視頻教程 VB.net 2010 視頻教程
        SQL Server 2008 視頻教程 c#入門經典教程 Visual Basic從門到精通視頻教程
        當前位置:
        首頁 > 編程開發 > C#編程 >
        • C#教程之C# 時間戳的相關操作

        • 2019-09-29 20:12 來源:未知

        一般的 時間戳 格式分為兩種 即 10位(秒)時間戳 與 13位(毫秒)時間戳

                    時間戳 類型也分為兩種 即 本地時間戳 與 世界統一(UTC)時間戳

        廢話不多說,直接上代碼:

        一、時間戳獲取方法

        復制代碼
        /// <summary>
        /// 獲取時間戳
        /// </summary>
        /// <param name="timeKind">時間類型(只能為 Local、Utc)</param>
        /// <param name="format">時間戳格式(只能為 10、13)</param>
        /// <returns></returns>
        private double GetTimestamp(int format, DateTimeKind timeKind)
        {
            TimeSpan timeSpan = new TimeSpan();
        
            switch (timeKind)
            {
                case DateTimeKind.Utc: timeSpan = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, timeKind); break;
                case DateTimeKind.Local: timeSpan = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, timeKind); break;
                default: throw new Exception("時間類型 只能為 Local、Utc");
            }
        
            switch (format)
            {
                case 10: return timeSpan.TotalSeconds;
                case 13: return timeSpan.TotalMilliseconds;
                default: throw new Exception("時間戳格式 只能為 10、13");
            }
        }
        
        /// <summary>
        /// 獲取10位時間戳
        /// </summary>
        /// <param name="timeKind">時間類型(只能為 Local、Utc,默認 Local)</param>
        /// <returns></returns>
        public int Get10Timestamp(DateTimeKind timeKind = DateTimeKind.Local)
        {
            return Convert.ToInt32(GetTimestamp(10, timeKind));
        }
        
        /// <summary>
        /// 獲取13位時間戳
        /// </summary>
        /// <param name="timeKind">時間類型(只能為 Local、Utc,默認 Local)</param>
        /// <returns></returns>
        public long Get13Timestamp(DateTimeKind timeKind = DateTimeKind.Local)
        {
            return Convert.ToInt64(GetTimestamp(13, timeKind));
        }
        復制代碼

        二、時間戳驗證方法

        復制代碼
        /// <summary>
        /// 驗證時間戳(10位、13位皆可)
        /// </summary>
        /// <param name="timestamp">時間戳</param>
        /// <param name="timeDiff">允許時差(10位時單位為 秒,13位時單位為 毫秒)</param>
        /// <param name="timeKind">時間類型(只能為 Local、Utc,默認 Local)</param>
        /// <returns></returns>
        public bool ValidateTimestamp(double timestamp, int timeDiff, DateTimeKind timeKind = DateTimeKind.Local)
        {
            TimeSpan timeSpan = new TimeSpan();
        
            switch (timeKind)
            {
                case DateTimeKind.Utc: timeSpan = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, timeKind); break;
                case DateTimeKind.Local: timeSpan = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, timeKind); break;
                default: throw new Exception("時間類型 只能為 Local、Utc");
            }
        
            double nowTimestamp = 0;  //現在的時間戳
            int format = timestamp.ToString("f0").Length;
        
            switch (format)
            {
                case 10: nowTimestamp = timeSpan.TotalSeconds; break;
                case 13: nowTimestamp = timeSpan.TotalMilliseconds; break;
                default: throw new Exception("時間戳格式 錯誤");
            }
        
            double nowTimeDiff = nowTimestamp - timestamp;  //現在的時差
        
            if (-timeDiff <= nowTimeDiff && nowTimeDiff <= timeDiff)
                return true;
            else
                return false;
        }
        復制代碼

        三、由 時間戳 轉換為 DateTime 方法

        復制代碼
        /// <summary>
        /// 將時間戳裝換為DateTime(10位、13位皆可)
        /// </summary>
        /// <param name="timestamp">時間戳</param>
        /// <param name="timeKind">時間類型(只能為 Local、Utc,默認 Local)</param>
        /// <param name="toTimeKind">返回的時間類型(只能為 Local、Utc,默認與 timeKind 一致)</param>
        /// <returns></returns>
        public DateTime TimestampToDateTime(double timestamp, DateTimeKind timeKind = DateTimeKind.Local, DateTimeKind toTimeKind = DateTimeKind.Unspecified)
        {
            DateTime startTime;
            toTimeKind = timeKind;
        
            switch (timeKind)
            {
                case DateTimeKind.Utc: startTime = new DateTime(1970, 1, 1, 0, 0, 0, timeKind); break;
                case DateTimeKind.Local: startTime = new DateTime(1970, 1, 1, 0, 0, 0, timeKind); break;
                default: throw new Exception("時間類型 只能為 Local、Utc");
            }
        
            DateTime newTime;
            int format = timestamp.ToString("f0").Length;
        
            switch (format)
            {
                case 10: newTime = startTime.AddSeconds(timestamp); break;
                case 13: newTime = startTime.AddMilliseconds(timestamp); break;
                default: throw new Exception("時間戳格式 錯誤");
            }
        
            if (newTime.Kind != toTimeKind)
                newTime = toTimeKind == DateTimeKind.Local ? newTime.ToLocalTime() : newTime.ToUniversalTime();
        
            return newTime;
        }
        復制代碼

        相關教程
        免费看成年人视频大全_免费看成年人视频在线观看