#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();
            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 = , , , , , );
            DateTime dt2 = , , );//2012-12-21 00:00:00
            Console.WriteLine(DateDiff(dt1, dt2));

            //我活了多少天了
            DateTime dt3 = , , , , , );
            DateTime dt4 = , , , , , );//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

以后忘记了直接查一下就好了

C#入门篇6-10:字符串操作 DateTime操作的更多相关文章

  1. C#入门篇6-2:字符串操作 string常用的函数

    //String 字符串的常见操作 public static void Fun1() { string MyStr = " Hello World! "; //length长度属 ...

  2. 【opencv入门篇】 10个程序快速上手opencv【下】

    导言:本系列博客目的在于能够在vs快速上手opencv,理论知识涉及较少,大家有兴趣可以查阅其他博客深入了解相关的理论知识,本博客后续也会对图像方向的理论进一步分析,敬请期待:) 上篇传送:http: ...

  3. 【opencv入门篇】 10个程序快速上手opencv【上】

    导言:本系列博客目的在于能够在vs快速上手opencv,理论知识涉及较少,大家有兴趣可以查阅其他博客深入了解相关的理论知识,本博客后续也会对图像方向的理论进一步分析,敬请期待:) PS:官方文档永远是 ...

  4. C#入门篇6-8:字符串操作 深入研究字符串的内存驻留机制

    //字符串的内存驻留机制 public static void Test() { //当有多个字符串变量包含了同样的字符串实际值时, //CLR可能不会为它们重复地分配内存,而是让它们统统指向同一个字 ...

  5. C#入门篇6-11:字符串操作 查找与替换

    #region 查找与替换 public class C4 { //查找 public static void StrFind() { //目标字符串 string str1 = "~awe ...

  6. C#入门篇6-9:字符串操作 不值一提的函数【不看也行】

    // 判断输入的是否全是数字:返回结果:true:全是数字:false:有字幕出现 public static bool Isaccord1(string str) { bool bl = true; ...

  7. C#入门篇6-7:字符串操作 看看字符串的特殊之处 值类型与引用类型的区别

    //看看字符串的特殊之处值类型与引用类型的区别 public static void CompareString(string stra, string strb, int i) { #region ...

  8. C#入门篇6-6:字符串操作 StringBiulder string char[]之间的转化

    //StringBiulder string char[]之间的转化 public static void Fun3() { StringBuilder sb = new StringBuilder( ...

  9. C#入门篇6-5:字符串操作 测试StringBuilder的运行效率

    //测试StringBuilder的运行效率 public static void Fun2() { #region string string str = "我喜欢编程!"; / ...

随机推荐

  1. Flex打印功能FlexPrintJob调用

    收藏下很详细的学习资料 http://wenku.baidu.com/view/b3d4f40702020740be1e9bf8.html import mx.printing.FlexPrintJo ...

  2. (三)主控板生级uboot与内核

    一.升级uboot 把我的电脑增加一个192.168.1.3的ip,方便升级boot. 1.连接串口,连接网口到GE1; 2.登陆电脑后到D:\MTG3000MAIN\first目录打开tftp32 ...

  3. (三)ubuntu学习前传—uboot常见环境变量

    1.环境变量如何参与程序运行(1)环境变量有2份,一份在Flash中,另一份在DDR中.uboot开机时一次性从Flash中读取全部环境变量到DDR中作为环境变量的初始化值,然后使用过程中都是用DDR ...

  4. Bootstrap强调相关的类

    在Bootstrap中除了使用标签<strong>.<em>等说明正文某些字词.句子的重要性,Bootstrap还定义了一套类名,这里称其为强调类名(类似前面说的“.lead” ...

  5. RAC 移动 OCR

    1.查看是否有OCR备份 # ocrconfig -showbackup 如果没有就备份一份 # ocrconfig -export /oracle/bak/ocr/ocr_11291433_exp. ...

  6. flume Permission denied: user=flume, access=WRITE, inode

    My flume app is attempting to write to HDFS on a path thats not been created/granted for it. The pat ...

  7. maven各种插件在总结

    http://blog.csdn.net/taiyangdao/article/category/6377863  好文章系列课程

  8. java 抽象类

    抽象类: 1)函数没有方法体,就必须用abstract修饰. 2)抽象函数所在的类必须也是抽象的. 3)非抽象的类继承于抽象类,必须实现其全部方法. 4)抽象类中可以存在抽象方法,也可以不存在. 5) ...

  9. 近期十大优秀jQuery插件推荐

    当有限的开发知识限制了设计进展,你无法为自己插上创新的翅膀时,jQuery可以扩展你的视野.本文将推荐从jQuery网站的Plugin频道中推选出的近期十款优秀jQuery插件. 1.jQuery U ...

  10. [svn] 分支开发

    参考博客: http://www.cnblogs.com/cxd4321/archive/2012/07/12/2588110.html (1)为什么要使用SVN分支开发和主干合并? 目的:在SVN下 ...