#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. Java中ArrayList相关的5道面试题

    本文参考了 <关于ArrayList的5道面试题 > 1.ArrayList的大小是如何自动增加的? 这个问题我想曾经debug过并且查看过arraylist源码的人都有印象,它的过程是: ...

  2. iOS - OC NSPoint 位置

    前言 结构体,这个结构体用来表示事物的一个坐标点. typedef CGPoint NSPoint; struct CGPoint { CGFloat x; CGFloat y; }; typedef ...

  3. java或者jsp中修复会话标识未更新漏洞

    AppScan会扫描“登录行为”前后的Cookie,其中会对其中的JSESSIONOID(或者别的cookie id依应用而定)进行记录.在登录行为发生后,如果cookie中这个值没有发生变化,则判定 ...

  4. DZY Loves Chessboard

    DescriptionDZY loves chessboard, and he enjoys playing with it. He has a chessboard of n rows and m ...

  5. hdu4418(概率dp + 高斯消元)

    应该是一个入门级别的题目. 但是有几个坑点. 1. 只选择x能到达的点作为guass中的未知数. 2. m可能大于n,所以在构建方程组时未知数的系数不能直接等于,要+= 3.题意貌似说的有问题,D为- ...

  6. android 存储

    总共四种:SharedPreferences,文件存储,SQLite数据库,ContentProvider,网络存储 1.sharedPreferences:适合存储少量数据,而且存取的格式简单,采用 ...

  7. 如何创建和使用XMLHttpRequest对象?

    创建XMLHttpRequest对象,我有以下几种方法,顺带给大家介绍下他们的使用,一起来看看那吧. 1. 第一种,我们可以使用构造函数的方式.直接new的方式,这样我们就构造了这个对象. reque ...

  8. 按钮靠右css小结

    按钮靠右 style="float:right"  ,多按钮排版会相反 按钮内的字体靠右 style="text-align:right" 按钮离右边框距离 s ...

  9. PCA in Bioinformatics

    光看原理和代码是没用的,得看看具体算法在实际中的用途,多看看文献. Principal component analysis for clustering gene expression data

  10. [ 转]Android快速开发–使用ORMLite操作数据库

    OrmLite是一个数据库操作辅助的开源框架,主要面向Java语言.在Android面向数据库开发中,是一个比较流行的开源框架,方便操作而且功能强大,今天来学习一下,最近的项目中也有所涉及,写个博客来 ...