欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#?時間戳轉(zhuǎn)換實例

 更新時間:2023年03月19日 15:00:42   作者:SCHOLAR_LIAO  
本文主要介紹了C#?時間戳轉(zhuǎn)換實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

本篇文章主要介紹了C# DateTime與時間戳(11位與13)轉(zhuǎn)換實例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧C#

        /// <summary>
        /// 將時間轉(zhuǎn)時間戳換
        /// 有效范圍1970-01-01 08:00:00~~2100-01-01 08:00:00 (范圍可改)
        /// </summary>
        /// <param name="time">要轉(zhuǎn)換的時間</param>
        /// <param name="length">輸出轉(zhuǎn)換長度</param> 
        /// <param name="timestamp">輸出時間戳</param>
        /// <returns></returns>
        public bool ToTimeStamp(DateTime time,int length, out long timestamp)
        {
            timestamp = 0;
            if (length == 11)
            {
                timestamp = Convert.ToInt64((time - (TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)))).TotalSeconds);
                return (timestamp > 0 && timestamp <= 4102444800);//范圍設定
            }
            else if (length == 13)
            {
                timestamp = Convert.ToInt64((time - (TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)))).TotalMilliseconds);
                return (timestamp > 0 && timestamp <= 4102444800000);//范圍設定
            }
            else
                return false;
        }
        /// <summary>
        /// 將時間戳換為時間轉(zhuǎn)
        /// 有效范圍1970-01-01 08:00:00~~2100-01-01 08:00:00 (范圍可改)
        /// </summary>
        /// <param name="timestamp">要轉(zhuǎn)換的時間戳</param>
        /// <param name="length">要轉(zhuǎn)換長度</param>
        /// <param name="time">輸出時間</param>
        /// <returns></returns>
        public bool ToDateTime(long timestamp, int length, out DateTime time)
        {
            time = DateTime.Now;
            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));//當?shù)貢r區(qū)
            if (length == 11)
            {
                time = startTime.AddSeconds(timestamp);
                return (timestamp > 0 && timestamp <= 4102444800);//范圍設定
            }
            else if (length == 13)
            {
                time = startTime.AddMilliseconds(timestamp);
                return (timestamp > 0 && timestamp <= 4102444800000);//范圍設定
            }
            else return false;
        }

            DateTime dt=DateTime.Now;
            new MyThread().ToTimeStamp(dt, 11, out long timestamp1);
            new MyThread().ToTimeStamp(dt, 13, out long timestamp2);
            new MyThread().ToDateTime(timestamp1, 11, out DateTime time1);
            new MyThread().ToDateTime(timestamp2, 13, out DateTime time2);
            Console.WriteLine("轉(zhuǎn)換的時間"+dt);
            Console.WriteLine($"11位的時間戳:{timestamp1}");
            Console.WriteLine($"13位的時間戳:{timestamp2}");
            Console.WriteLine($"11位的時間戳轉(zhuǎn)時間:{time1}");
            Console.WriteLine($"13位的時間戳轉(zhuǎn)時間:{time2}");

調(diào)用結(jié)果

到此這篇關于C# 時間戳轉(zhuǎn)換實例的文章就介紹到這了,更多相關C# 時間戳轉(zhuǎn)換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論