亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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

        /// <summary>
        /// 將時(shí)間轉(zhuǎn)時(shí)間戳換
        /// 有效范圍1970-01-01 08:00:00~~2100-01-01 08:00:00 (范圍可改)
        /// </summary>
        /// <param name="time">要轉(zhuǎn)換的時(shí)間</param>
        /// <param name="length">輸出轉(zhuǎn)換長(zhǎng)度</param> 
        /// <param name="timestamp">輸出時(shí)間戳</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);//范圍設(shè)定
            }
            else if (length == 13)
            {
                timestamp = Convert.ToInt64((time - (TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)))).TotalMilliseconds);
                return (timestamp > 0 && timestamp <= 4102444800000);//范圍設(shè)定
            }
            else
                return false;
        }
        /// <summary>
        /// 將時(shí)間戳換為時(shí)間轉(zhuǎn)
        /// 有效范圍1970-01-01 08:00:00~~2100-01-01 08:00:00 (范圍可改)
        /// </summary>
        /// <param name="timestamp">要轉(zhuǎn)換的時(shí)間戳</param>
        /// <param name="length">要轉(zhuǎn)換長(zhǎng)度</param>
        /// <param name="time">輸出時(shí)間</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));//當(dāng)?shù)貢r(shí)區(qū)
            if (length == 11)
            {
                time = startTime.AddSeconds(timestamp);
                return (timestamp > 0 && timestamp <= 4102444800);//范圍設(shè)定
            }
            else if (length == 13)
            {
                time = startTime.AddMilliseconds(timestamp);
                return (timestamp > 0 && timestamp <= 4102444800000);//范圍設(shè)定
            }
            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)換的時(shí)間"+dt);
            Console.WriteLine($"11位的時(shí)間戳:{timestamp1}");
            Console.WriteLine($"13位的時(shí)間戳:{timestamp2}");
            Console.WriteLine($"11位的時(shí)間戳轉(zhuǎn)時(shí)間:{time1}");
            Console.WriteLine($"13位的時(shí)間戳轉(zhuǎn)時(shí)間:{time2}");

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

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

相關(guān)文章

最新評(píng)論