亲宝软件园·资讯

展开

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

人气:2
想了解c# DateTime常用操作实例(datetime计算时间差)的相关内容吗,在本文为您仔细讲解DateTime常用操作的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:datetime计算时间差,下面大家一起来学习吧。

复制代码 代码如下:

#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, 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));
        }

        //计算时间的差值
        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

加载全部内容

相关教程
猜你喜欢
用户评论