#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. scala中如何编写自定义的流程控制结构

    scala是一种函数式编程风格的语言,除了常见的if......else  ,for ,while等传统的流程控制结构,也可以自定义流程控制的控制结构. 再了解scala如何实现编写新的流程结构,我们 ...

  2. Nginx基础知识之————Nginx 环境的搭建?

    本课时主要给大家讲解如何在 Linux 系统下搭建 Nginx 和 Nginx 搭建过程中常见问题的知识,并结合实例让学员掌握 Nginx 环境的搭建. 下载解压: 安装gcc-c++ 从新配置文件: ...

  3. ubuntu安装jdk-6u45-linux-x64.bin___ZC_20160423

    for : Android4.4源码编译 环境 : ubuntu12.04_desktop_amd64 1. 1.1.jdk-6u45-linux-x64.bin 放置于 /home 1.2.命令&q ...

  4. [转载] google mock cookbook

    原文: https://code.google.com/p/googlemock/wiki/CookBook Creating Mock Classes Mocking Private or Prot ...

  5. elastic

    学习链接 http://rfyiamcool.blog.51cto.com/1030776/1420811?utm_source=tuicool&utm_medium=referral

  6. Binary Tree Paths

    Description: Given a binary tree, return all root-to-leaf paths. For example, given the following bi ...

  7. C++——代码重用

    一.包含对象成员的类 接口和实现:使用公有继承时,类可以继承接口,可能还有实现(基类的纯虚函数提供接口,但不提供实现).获得接口是is-a关系的组成部分.而使用组合,类可以获得实现,但不能获得接口.不 ...

  8. golang type 和断言 interface{}转换

    摘要 类型转换在程序设计中都是不可避免的问题.当然有一些语言将这个过程给模糊了,大多数时候开发者并不需要去关 注这方面的问题.但是golang中的类型匹配是很严格的,不同的类型之间通常需要手动转换,编 ...

  9. Android控件之AutoCompleteTextView(自动匹配输入的内容)

    一.功能 动态匹配输入的内容,如百度搜索引擎当输入文本时,可以根据内容显示匹配的热门信息 二.独特属性 android:completionThreshold = "2"    — ...

  10. jquery中html(), text(),val()区别(zhuan)

    https://zhidao.baidu.com/question/307317838.html http://www.cnblogs.com/aqbyygyyga/archive/2011/11/0 ...