加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# DateTime常用操作实例(datetime计算时间差)

发布时间:2020-12-15 05:58:14 所属栏目:百科 来源:网络整理
导读:复制代码 代码如下: #region DateTime操作 public class C3 { //DateTime常用的操作 public static void Fun1() { //格式:2012-8-16 11:21:29 Console.WriteLine("当前时间:{0}",DateTime.Now.ToString()); //格式:2012-8-16 0:00:00 Console.WriteLine("

复制代码 代码如下:

#region DateTime操作

    public class C3
    {
        //DateTime常用的操作
        public static void Fun1()
        {
            //格式:2012-8-16 11:21:29
            Console.WriteLine("当前时间:{0}",DateTime.Now.ToString());

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

            //格式:11:21:29
            Console.WriteLine("时间部分:{0}",DateTime.Now.ToLongTimeString());

            //获取此实例的当天的时间。相当的精确【到毫秒】
            Console.WriteLine("TimeOfDay:{0}",DateTime.Now.TimeOfDay.ToString());

            Console.WriteLine("取中文日期显示_年月日时分:{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"));

            //取当前时分,格式为:14:24
            Console.WriteLine("取当前时分:{0}",DateTime.Now.ToString("t"));

            //取当前时间,格式为:2003-09-23T14:46:48
            Console.WriteLine("取当前时分:{0}",DateTime.Now.ToString("s"));

            //取当前时间,格式为:2003-09-23 14:48:30Z
            Console.WriteLine("取当前时分:{0}",DateTime.Now.ToString("u"));

            //取当前时间,格式为:2003-09-23 14:48
            Console.WriteLine("取当前时分:{0}",DateTime.Now.ToString("g"));

            //取当前时间,格式为:Tue,23 Sep 2003 14:52:40 GMT
            Console.WriteLine("取当前时分:{0}",DateTime.Now.ToString("r"));

            //获得当前时间 n 天后的日期时间
            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("时:{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("计时周期数:{0}",DateTime.Now.Ticks.ToString());
            Console.WriteLine("星期:{0}",DateTime.Now.DayOfWeek.ToString());
            Console.WriteLine("一年中的第几天:{0}",DateTime.Now.DayOfYear.ToString());

        }

        //客户端代码
        public static void MyFun()
        {
            //struct本身是一个结构体
            //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,00,00);
            DateTime dt4 = new DateTime(1990,11,17,02,48,00);//2012-12-21 00:00:00
            Console.WriteLine("我活了多少天" + DateDiff(dt4,dt3));
        }

        //计算时间的差值
        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() + "小时" + ts.Minutes.ToString() + "分钟" + ts.Seconds.ToString() + "秒";
            return dateDiff;

            #region note
            //C#中使用TimeSpan计算两个时间的差值
            //可以反加两个日期之间任何一个时间单位。
            //TimeSpan ts = Date1 - Date2;
            //double dDays = ts.TotalDays;//带小数的天数,比如1天12小时结果就是1.5
            //int nDays = ts.Days;//整数天数,1天12小时或者1天20小时结果都是1 
            #endregion
        }
    }

    #endregion

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读