1. 获得当前系统时间: DateTime dt = DateTime.Now;
  2. Environment.TickCount可以得到“系统启动到现在”的毫秒值
  3. DateTime now = DateTime.Now;
  4. Console.WriteLine(now.ToString("yyyy-MM-dd")); //按yyyy-MM-dd格式输出s
  5. Console.WriteLine(dt.ToString()); // 26/11/2009 AM 11:21:30
  6. Console.WriteLine(dt.ToFileTime().ToString()); // 129036792908014024
  7. // Converts the value of the current System.DateTime object to a Windows file time
  8. Console.WriteLine(dt.ToFileTimeUtc().ToString()); // 129036792908014024
  9. // Converts the value of the current System.DateTime object to a Windows file time
  10. Console.WriteLine(dt.ToLocalTime().ToString()); // 26/11/2009 AM 11:21:30
  11. // Converts the value of the current System.DateTime object to local time.
  12. Console.WriteLine(dt.ToLongDateString().ToString()); // 2009年11月26日
  13. Console.WriteLine(dt.ToLongTimeString().ToString()); // AM 11:21:30
  14. Console.WriteLine(dt.ToOADate().ToString()); // 40143.4732731597
  15. Console.WriteLine(dt.ToShortDateString().ToString()); // 26/11/2009
  16. Console.WriteLine(dt.ToShortTimeString().ToString()); // AM 11:21
  17. Console.WriteLine(dt.ToUniversalTime().ToString()); // 26/11/2009 AM 3:21:30
  18. Console.WriteLine(dt.Year.ToString()); // 2009
  19. Console.WriteLine(dt.Date.ToString()); // 26/11/2009 AM 12:00:00
  20. Console.WriteLine(dt.DayOfWeek.ToString()); // Thursday
  21. Console.WriteLine(dt.DayOfYear.ToString()); // 330
  22. Console.WriteLine(dt.Hour.ToString()); // 11
  23. Console.WriteLine(dt.Millisecond.ToString()); // 801 (毫秒)
  24. Console.WriteLine(dt.Minute.ToString()); // 21
  25. Console.WriteLine(dt.Month.ToString()); // 11
  26. Console.WriteLine(dt.Second.ToString()); // 30
  27. Console.WriteLine(dt.Ticks.ToString()); // 633948312908014024
  28.  
  29. Console.WriteLine(dt.TimeOfDay.ToString()); // 12:29:51.5181524
  30. // Gets the time of day for this instance.
  31. // 返回 A System.TimeSpan that represents the fraction of the day that has elapsed since midnight.
  32. Console.WriteLine(dt.ToString()); // 26/11/2009 PM 12:29:51
  33. Console.WriteLine(dt.AddYears(1).ToString()); // 26/11/2010 PM 12:29:51
  34. Console.WriteLine(dt.AddDays(1.1).ToString()); // 27/11/2009 PM 2:53:51
  35. Console.WriteLine(dt.AddHours(1.1).ToString()); // 26/11/2009 PM 1:35:51
  36. Console.WriteLine(dt.AddMilliseconds(1.1).ToString()); //26/11/2009 PM 12:29:51
  37. Console.WriteLine(dt.AddMonths(1).ToString()); // 26/12/2009 PM 12:29:51
  38. Console.WriteLine(dt.AddSeconds(1.1).ToString()); // 26/11/2009 PM 12:29:52
  39. Console.WriteLine(dt.AddMinutes(1.1).ToString()); // 26/11/2009 PM 12:30:57
  40. Console.WriteLine(dt.AddTicks(1000).ToString()); // 26/11/2009 PM 12:29:51
  41. Console.WriteLine(dt.CompareTo(dt).ToString()); // 0
  42. Console.WriteLine(dt.Add(new TimeSpan(1,0,0,0)).ToString()); // 加上一个时间段
  43. (注:
  44. System.TimeSpan为一个时间段,构造函数如下
  45. public TimeSpan(long ticks); // ticks: A time period expressed in 100-nanosecond units.
  46. //nanosecond:十亿分之一秒 new TimeSpan(10,000,000) 为一秒
  47. public TimeSpan(int hours, int minutes, int seconds);
  48. public TimeSpan(int days, int hours, int minutes, int seconds);
  49. public TimeSpan(int days, int hours, int minutes, int seconds, int milliseconds);

  50. Console.WriteLine(dt.Equals("2005-11-6 16:11:04").ToString()); // False
  51. Console.WriteLine(dt.Equals(dt).ToString()); // True
  52. Console.WriteLine(dt.GetHashCode().ToString()); // 1103291775
  53. Console.WriteLine(dt.GetType().ToString()); // System.DateTime
  54. Console.WriteLine(dt.GetTypeCode().ToString()); // DateTime
  55.  
  56. long Start = Environment.TickCount; //单位是毫秒
  57. long End = Environment.TickCount;
  58. Console.WriteLine("Start is : "+Start);
  59. Console.WriteLine("End is : "+End);
  60. Console.WriteLine("The Time is {0}",End-Start);
  61. Console.WriteLine(dt.GetDateTimeFormats('s')[0].ToString()); //2009-11-26T13:29:06
  62. Console.WriteLine(dt.GetDateTimeFormats('t')[0].ToString()); //PM 1:29
  63. Console.WriteLine(dt.GetDateTimeFormats('y')[0].ToString()); //2009年11月
  64. Console.WriteLine(dt.GetDateTimeFormats('D')[0].ToString()); //2009年11月26日
  65. Console.WriteLine(dt.GetDateTimeFormats('D')[1].ToString()); //星期四, 26 十一月, 2009
  66. Console.WriteLine(dt.GetDateTimeFormats('D')[2].ToString()); //26 十一月, 2009
  67. Console.WriteLine(dt.GetDateTimeFormats('D')[3].ToString()); //星期四 2009 11 26
  68. Console.WriteLine(dt.GetDateTimeFormats('M')[0].ToString()); //26 十一月
  69. Console.WriteLine(dt.GetDateTimeFormats('f')[0].ToString()); //2009年11月26日 PM 1:29
  70. Console.WriteLine(dt.GetDateTimeFormats('g')[0].ToString()); //26/11/2009 PM 1:29
  71. Console.WriteLine(dt.GetDateTimeFormats('r')[0].ToString()); //Thu, 26 Nov 2009 13:29:06 GMT
  72. (注:
  73. 常用的日期时间格式:
  74. 格式 说明 输出格式
  75. d 精简日期格式 MM/dd/yyyy
  76. D 详细日期格式 dddd, MMMM dd, yyyy
  77. f 完整格式 (long date + short time) dddd, MMMM dd, yyyy HH:mm
  78. F 完整日期时间格式 (long date + long time) dddd, MMMM dd, yyyy HH:mm:ss
  79. g 一般格式 (short date + short time) MM/dd/yyyy HH:mm
  80. G 一般格式 (short date + long time) MM/dd/yyyy HH:mm:ss
  81. m,M 月日格式 MMMM dd
  82. s 适中日期时间格式 yyyy-MM-dd HH:mm:ss
  83. t 精简时间格式 HH:mm
  84. T 详细时间格式 HH:mm:ss
  85. )
  86.  
  87. Console.WriteLine(string.Format("{0:d}", dt)); //28/12/2009
  88. Console.WriteLine(string.Format("{0:D}", dt)); //2009年12月28日
  89. Console.WriteLine(string.Format("{0:f}", dt)); //2009年12月28日 AM 10:29
  90. Console.WriteLine(string.Format("{0:F}", dt)); //2009年12月28日 AM 10:29:18
  91. Console.WriteLine(string.Format("{0:g}", dt)); //28/12/2009 AM 10:29
  92. Console.WriteLine(string.Format("{0:G}", dt)); //28/12/2009 AM 10:29:18
  93. Console.WriteLine(string.Format("{0:M}", dt)); //28 十二月
  94. Console.WriteLine(string.Format("{0:R}", dt)); //Mon, 28 Dec 2009 10:29:18 GMT
  95. Console.WriteLine(string.Format("{0:s}", dt)); //2009-12-28T10:29:18
  96. Console.WriteLine(string.Format("{0:t}", dt)); //AM 10:29
  97. Console.WriteLine(string.Format("{0:T}", dt)); //AM 10:29:18
  98. Console.WriteLine(string.Format("{0:u}", dt)); //2009-12-28 10:29:18Z
  99. Console.WriteLine(string.Format("{0:U}", dt)); //2009年12月28日 AM 2:29:18
  100. Console.WriteLine(string.Format("{0:Y}", dt)); //2009年12月
  101. Console.WriteLine(string.Format("{0}", dt)); //28/12/2009 AM 10:29:18
  102. Console.WriteLine(string.Format("{0:yyyyMMddHHmmssffff}", dt)); //200912281029182047
  103.  
  104. 计算2个日期之间的天数差
  105. DateTime dt1 = Convert.ToDateTime("2007-8-1");
  106. DateTime dt2 = Convert.ToDateTime("2007-8-15");
  107. TimeSpan span = dt2.Subtract(dt1);
  108. int dayDiff = span.Days ;
  109.  
  110. 计算某年某月的天数
  111. int days = DateTime.DaysInMonth(2009, 8);
  112. days = 31;
  113.  
  114. 给日期增加一天、减少一天
  115. DateTime dt =DateTime.Now;
  116. dt.AddDays(1); //增加一天 dt本身并不改变
  117. dt.AddDays(-1);//减少一天 dt本身并不改变

C# DateTime的使用的更多相关文章

  1. C# DateTime与时间戳转换

    C# DateTime与时间戳的相互转换,包括JavaScript时间戳和Unix的时间戳. 1. 什么是时间戳 首先要清楚JavaScript与Unix的时间戳的区别: JavaScript时间戳: ...

  2. C# DateTime日期格式化

    在C#中DateTime是一个包含日期.时间的类型,此类型通过ToString()转换为字符串时,可根据传入给Tostring()的参数转换为多种字符串格式. 目录 1. 分类 2. 制式类型 3. ...

  3. 在面试中忽然发现DateTime的一些...

    今天说说我面试中碰到的一个小问题,在我问起DateTime为什么无法赋值NULL值,一般第一反应都认为它是值类型,不是引用类型,但随后我查阅了度娘自我学习到它是结构类型,那么随之而然就无法赋值NULL ...

  4. LINQ to SQL语句(14)之Null语义和DateTime

    Null语义 说明:下面第一个例子说明查询ReportsToEmployee为null的雇员.第二个例子使用Nullable<T>.HasValue查询雇员,其结果与第一个例子相同.在第三 ...

  5. .NET DateTime类型变量作为参数时设置默认值

    一个小的 Tips. .NET 中函数参数的默认值需要是编译时常量.如果参数是引用类型,可以设置Null,如果是值类型,可以设置相应的编译时常量,如整型可以用整数,但对于DateTime(结构体,值类 ...

  6. BCS datetime 时间区间问题

    BCS 整合sql表时发现以下问题: datetime字段在列表中带了时区,比如插入12-6号的数据,在sql中显示的是12-5 date类型字段无法正确识别,插入成功但报错 LobSystem (外 ...

  7. C#中DateTime.Ticks属性及Unix时间戳转换

    1.相关概念 DateTime.Ticks:表示0001 年 1 月 1 日午夜 12:00:00 以来所经历的 100 纳秒数,即Ticks的属性为100纳秒(1Ticks = 0.0001毫秒). ...

  8. WPF 自定义DateControl DateTime控件

    自定义日期控件,月份选择.如下是日期的一些效果图. 具体的样式.颜色可以根据下面的代码,自己调节即可    1.日期控件的界面 <UserControl x:Class="WpfApp ...

  9. JavaScript 解析 Django Python 生成的 datetime 数据 时区问题解决

    JavaScript 解析 Django/Python 生成的 datetime 数据 当Web后台使用Django时,后台生成的时间数据类型就是Python类型的. 项目需要将几个时间存储到数据库中 ...

  10. python标准模块(time、datetime及hashlib模块)

    一.time,datetime模块 时间相关的操作 import time time.sleep(5) # ==> 停顿多少秒 print(time.time()) # ==> 返回时间戳 ...

随机推荐

  1. php类内方法使用类外变量和类外函数

    如果要调用另一个类的属性和方法,直接实例化后调用就可以 但是要使用的变量和函数是独立的,不在本类或其他类里面,这时调用就要用下面的方法(本例是在同一个php文件里,如果不再同一个类文件可以requir ...

  2. 二次开发php

    AB模板  http://www.adminbuy.cn/ 码源 (站长之家)http://down.chinaz.com/ 易无袖资源 http://www.ewuxiu.com/ A5码源 htt ...

  3. SpringMVC(二)高级应用

    一.参数绑定-----集合类型 二.数据回显(例如提交表单失败了,数据没有丢失) 三.上传图片 四.json数据的交互 五.restful 支持 六.拦截器

  4. LeetCode 069 Sqrt(x) 求平方根

    Implement int sqrt(int x).Compute and return the square root of x.x is guaranteed to be a non-negati ...

  5. 028 Implement strStr() 实现 strStr()

    实现 strStr().返回蕴含在 haystack 中的 needle 的第一个字符的索引,如果 needle 不是 haystack 的一部分则返回 -1 .例 1:输入: haystack = ...

  6. ForkJoin有参无返回值、有参有返回值实例

    介绍: a . Fork/Join为JKD1.7引入,适用于对大量数据进行拆分成多个小任务进行计算的框架,最后把所有小任务的结果汇总合并得到最终的结果 b . 相关类 public abstract ...

  7. 引用 Session详解 作者:郎云鹏

    本文转载自leeldy<Session详解 作者:郎云鹏>   引用 leeldy 的 Session详解 作者:郎云鹏 目录: 一.术语session 二.HTTP协议与状态保持 三.理 ...

  8. MVC中 Remote的用法

    一.web.config加入  <appSettings>    <add key="ClientValidationEnabled" value="t ...

  9. 虚拟机安装CentOS7 Minimal、jdk和hadoop

    虚拟机安装CentOS7 Minimal.jdk和hadoop Table of Contents 1. 安装版本 2. PD安装 3. vim安装和配置 4. 主机名变为bogon的解决办法 5. ...

  10. lambda匿名函数,sorted排序,filter()筛选,map()映射

    一丶匿名函数 语法: 函数名 = lambda参数:返回值 # 普通的正常的函数 def func(n): return n * n ret = func(9) print(ret) # 匿名函数 a ...