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

c# DateTime常用操作實(shí)例(datetime計(jì)算時(shí)間差)

 更新時(shí)間:2013年12月26日 15:12:50   作者:  
字符串操作DateTime操作,datetime計(jì)算時(shí)間差,取當(dāng)前時(shí)間,更多方法看下面代碼

復(fù)制代碼 代碼如下:

#region DateTime操作

    public class C3
    {
        //DateTime常用的操作
        public static void Fun1()
        {
            //格式:2012-8-16 11:21:29
            Console.WriteLine("當(dāng)前時(shí)間:{0}", DateTime.Now.ToString());

            //格式:2012-8-16 0:00:00
            Console.WriteLine("日期部分:{0}", DateTime.Now.Date.ToString());

            //格式:11:21:29
            Console.WriteLine("時(shí)間部分:{0}", DateTime.Now.ToLongTimeString());

            //獲取此實(shí)例的當(dāng)天的時(shí)間。相當(dāng)?shù)木_【到毫秒】
            Console.WriteLine("TimeOfDay:{0}", DateTime.Now.TimeOfDay.ToString());

            Console.WriteLine("取中文日期顯示_年月日時(shí)分:{0}", DateTime.Now.ToString("f"));

            Console.WriteLine("取中文日期顯示_年月:{0}", DateTime.Now.ToString("y"));

            Console.WriteLine("取中文日期顯示_月日:{0}", DateTime.Now.ToString("m"));

            Console.WriteLine("取中文年月日:{0}", DateTime.Now.ToString("D"));

            //取當(dāng)前時(shí)分,格式為:14:24
            Console.WriteLine("取當(dāng)前時(shí)分:{0}", DateTime.Now.ToString("t"));

            //取當(dāng)前時(shí)間,格式為:2003-09-23T14:46:48
            Console.WriteLine("取當(dāng)前時(shí)分:{0}", DateTime.Now.ToString("s"));

            //取當(dāng)前時(shí)間,格式為:2003-09-23 14:48:30Z
            Console.WriteLine("取當(dāng)前時(shí)分:{0}", DateTime.Now.ToString("u"));

            //取當(dāng)前時(shí)間,格式為:2003-09-23 14:48
            Console.WriteLine("取當(dāng)前時(shí)分:{0}", DateTime.Now.ToString("g"));

            //取當(dāng)前時(shí)間,格式為:Tue, 23 Sep 2003 14:52:40 GMT
            Console.WriteLine("取當(dāng)前時(shí)分:{0}", DateTime.Now.ToString("r"));

            //獲得當(dāng)前時(shí)間 n 天后的日期時(shí)間
            DateTime newDay = DateTime.Now.AddDays(100);
            Console.WriteLine(newDay.ToString());

            Console.WriteLine("年:{0}", DateTime.Now.Year.ToString());
            Console.WriteLine("月:{0}", DateTime.Now.Month.ToString());
            Console.WriteLine("日:{0}", DateTime.Now.Day.ToString());
            Console.WriteLine("時(shí):{0}", DateTime.Now.Hour.ToString());
            Console.WriteLine("分:{0}", DateTime.Now.Minute.ToString());
            Console.WriteLine("秒:{0}", DateTime.Now.Second.ToString());
            Console.WriteLine("毫秒:{0}", DateTime.Now.Millisecond.ToString());

            Console.WriteLine("計(jì)時(shí)周期數(shù):{0}", DateTime.Now.Ticks.ToString());
            Console.WriteLine("星期:{0}", DateTime.Now.DayOfWeek.ToString());
            Console.WriteLine("一年中的第幾天:{0}", DateTime.Now.DayOfYear.ToString());

        }

        //客戶端代碼
        public static void MyFun()
        {
            //struct本身是一個(gè)結(jié)構(gòu)體
            //DateTime dt0 = new DateTime();

            DateTime dt1 = new DateTime(2012, 8, 14, 10, 54, 55);
            DateTime dt2 = new DateTime(2012, 12, 21);//2012-12-21 00:00:00
            Console.WriteLine(DateDiff(dt1, dt2));

            //我活了多少天了
            DateTime dt3 = new DateTime(2012, 8, 14, 12, 00, 00);
            DateTime dt4 = new DateTime(1990, 11, 17, 02, 48, 00);//2012-12-21 00:00:00
            Console.WriteLine("我活了多少天" + DateDiff(dt4, dt3));
        }

        //計(jì)算時(shí)間的差值
        public static string DateDiff(DateTime DateTime1, DateTime DateTime2)
        {
            string dateDiff = null;
            TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
            TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);

            TimeSpan ts = ts1.Subtract(ts2).Duration();

            dateDiff = ts.Days.ToString() + "天" + ts.Hours.ToString() + "小時(shí)" + ts.Minutes.ToString() + "分鐘" + ts.Seconds.ToString() + "秒";
            return dateDiff;

            #region note
            //C#中使用TimeSpan計(jì)算兩個(gè)時(shí)間的差值
            //可以反加兩個(gè)日期之間任何一個(gè)時(shí)間單位。
            //TimeSpan ts = Date1 - Date2;
            //double dDays = ts.TotalDays;//帶小數(shù)的天數(shù),比如1天12小時(shí)結(jié)果就是1.5
            //int nDays = ts.Days;//整數(shù)天數(shù),1天12小時(shí)或者1天20小時(shí)結(jié)果都是1 
            #endregion
        }
    }

    #endregion

相關(guān)文章

  • C#調(diào)用OpenCV開發(fā)簡易版美圖工具【推薦】

    C#調(diào)用OpenCV開發(fā)簡易版美圖工具【推薦】

    本文主要介紹在WPF項(xiàng)目中使用OpenCVSharp3-AnyCPU開源類庫處理圖片,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2019-10-10
  • c#使用正則表達(dá)式匹配字符串驗(yàn)證URL示例

    c#使用正則表達(dá)式匹配字符串驗(yàn)證URL示例

    這篇文章主要介紹了c#使用正則表達(dá)式的小示例,匹配字符串、驗(yàn)證URL,大家參考使用吧
    2013-12-12
  • C#實(shí)現(xiàn)串口示波器

    C#實(shí)現(xiàn)串口示波器

    這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)串口示波器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • C#簡單實(shí)現(xiàn)發(fā)送socket字符串

    C#簡單實(shí)現(xiàn)發(fā)送socket字符串

    這篇文章主要為大家詳細(xì)介紹了C#簡單實(shí)現(xiàn)socket字符串發(fā)送,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-09-09
  • C# memcached緩存使用實(shí)例代碼

    C# memcached緩存使用實(shí)例代碼

    這篇文章主要介紹了C# memcached緩存使用實(shí)例,大家參考使用
    2013-12-12
  • c#操作Redis的5種基本類型匯總

    c#操作Redis的5種基本類型匯總

    這篇文章主要給大家介紹了關(guān)于c#操作Redis的5種基本類型,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用C#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • Unity3D實(shí)現(xiàn)甜品消消樂游戲

    Unity3D實(shí)現(xiàn)甜品消消樂游戲

    這篇文章主要介紹了通過C# Unity3D繪制一個(gè)甜品消消樂游戲,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定的幫助,感興趣的小伙伴可以學(xué)習(xí)一下
    2021-12-12
  • 詳解如何獲取C#類中發(fā)生數(shù)據(jù)變化的屬性信息

    詳解如何獲取C#類中發(fā)生數(shù)據(jù)變化的屬性信息

    這篇文章主要介紹了詳解如何獲取C#類中發(fā)生數(shù)據(jù)變化的屬性信息,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • C#中事務(wù)處理和非事務(wù)處理方法實(shí)例分析

    C#中事務(wù)處理和非事務(wù)處理方法實(shí)例分析

    這篇文章主要介紹了C#中事務(wù)處理和非事務(wù)處理方法,較為詳細(xì)的分析了C#中事務(wù)處理與非事務(wù)處理的使用技巧,對(duì)于使用C#進(jìn)行數(shù)據(jù)庫程序開發(fā)有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07
  • C#中的Internal關(guān)鍵字小結(jié)

    C#中的Internal關(guān)鍵字小結(jié)

    這篇文章主要介紹了C#中的Internal關(guān)鍵字小結(jié)的相關(guān)資料,需要的朋友可以參考下
    2017-05-05

最新評(píng)論