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()); //
  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()); //
  22. Console.WriteLine(dt.Hour.ToString()); //
  23. Console.WriteLine(dt.Millisecond.ToString()); // 801 (毫秒)
  24. Console.WriteLine(dt.Minute.ToString()); //
  25. Console.WriteLine(dt.Month.ToString()); //
  26. Console.WriteLine(dt.Second.ToString()); //
  27. Console.WriteLine(dt.Ticks.ToString()); //
  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().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().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().ToString()); // 26/11/2009 PM 12:29:51
  41. Console.WriteLine(dt.CompareTo(dt).ToString()); //
  42. Console.WriteLine(dt.Add(new TimeSpan(,,,)).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()); //
  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')[].ToString()); //2009-11-26T13:29:06
  62. Console.WriteLine(dt.GetDateTimeFormats('t')[].ToString()); //PM 1:29
  63. Console.WriteLine(dt.GetDateTimeFormats('y')[].ToString()); //2009年11月
  64. Console.WriteLine(dt.GetDateTimeFormats('D')[].ToString()); //2009年11月26日
  65. Console.WriteLine(dt.GetDateTimeFormats('D')[].ToString()); //星期四, 26 十一月, 2009
  66. Console.WriteLine(dt.GetDateTimeFormats('D')[].ToString()); //26 十一月, 2009
  67. Console.WriteLine(dt.GetDateTimeFormats('D')[].ToString()); //星期四 2009 11 26
  68. Console.WriteLine(dt.GetDateTimeFormats('M')[].ToString()); //26 十一月
  69. Console.WriteLine(dt.GetDateTimeFormats('f')[].ToString()); //2009年11月26日 PM 1:29
  70. Console.WriteLine(dt.GetDateTimeFormats('g')[].ToString()); //26/11/2009 PM 1:29
  71. Console.WriteLine(dt.GetDateTimeFormats('r')[].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)); //
  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(, );
  112. days = ;
  113.  
  114. 给日期增加一天、减少一天
  115. DateTime dt =DateTime.Now;
  116. dt.AddDays(); //增加一天 dt本身并不改变
  117. dt.AddDays(-);//减少一天 dt本身并不改变
  118.  
  119. 参数format格式详细用法:
  120.  
  121. 格式字符关联属性/说明
  122. dShortDatePattern
  123. DLongDatePattern
  124. f完整日期和时间(长日期和短时间)
  125. FFullDateTimePattern(长日期和长时间)
  126. g常规(短日期和短时间)
  127. G常规(短日期和长时间)
  128. mMMonthDayPattern
  129. rRRFC1123Pattern
  130. s使用当地时间的SortableDateTimePattern(基于ISO8601
  131. tShortTimePattern
  132. TLongTimePattern
  133. uUniversalSortableDateTimePattern用于显示通用时间的格式
  134. U使用通用时间的完整日期和时间(长日期和长时间)
  135. yYYearMonthPattern
  136.  
  137. 下面列出可被合并以构造自定义模式的模式:
  138.  
  139. d月中的某一天。一位数的日期没有前导零。
  140. dd月中的某一天。一位数的日期有一个前导零。
  141. ddd周中某天的缩写名称,在AbbreviatedDayNames中定义。
  142. dddd周中某天的完整名称,在DayNames中定义。
  143. M月份数字。一位数的月份没有前导零。
  144. MM月份数字。一位数的月份有一个前导零。
  145. MMM月份的缩写名称,在AbbreviatedMonthNames中定义。
  146. MMMM月份的完整名称,在MonthNames中定义。
  147. y不包含纪元的年份。如果不包含纪元的年份小于10,则显示不具有前导零的年份。
  148. yy不包含纪元的年份。如果不包含纪元的年份小于10,则显示具有前导零的年份。
  149. yyyy包括纪元的四位数的年份。
  150. gg时期或纪元。如果要设置格式的日期不具有关联的时期或纪元字符串,则忽略该模式。
  151. h12小时制的小时。一位数的小时数没有前导零。
  152. hh12小时制的小时。一位数的小时数有前导零。
  153. H24小时制的小时。一位数的小时数没有前导零。
  154. HH24小时制的小时。一位数的小时数有前导零。
  155. m分钟。一位数的分钟数没有前导零。
  156. mm分钟。一位数的分钟数有一个前导零。
  157. s秒。一位数的秒数没有前导零。
  158. ss秒。一位数的秒数有一个前导零。
  159. f秒的小数精度为一位。其余数字被截断。
  160. ff秒的小数精度为两位。其余数字被截断。
  161. fff秒的小数精度为三位。其余数字被截断。
  162. ffff秒的小数精度为四位。其余数字被截断。
  163. fffff秒的小数精度为五位。其余数字被截断。
  164. ffffff秒的小数精度为六位。其余数字被截断。
  165. fffffff秒的小数精度为七位。其余数字被截断。
  166. tAMDesignatorPMDesignator中定义的AM/PM指示项的第一个字符(如果存在)。
  167. ttAMDesignatorPMDesignator中定义的AM/PM指示项(如果存在)。
  168. z时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数没有前导零。例如,太平洋标准时间是“-”。
  169. zz时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数有前导零。例如,太平洋标准时间是“-”。
  170. zzz完整时区偏移量(“+”或“-”后面跟有小时和分钟)。一位数的小时数和分钟数有前导零。例如,太平洋标准时间是“-:”。
  171. :在TimeSeparator中定义的默认时间分隔符。
  172. /在DateSeparator中定义的默认日期分隔符。
  173. %c其中c是格式模式(如果单独使用)。如果格式模式与原义字符或其他格式模式合并,则可以省略“%”字符。
  174. \c其中c是任意字符。照原义显示字符。若要显示反斜杠字符,请使用“\\”。

C# Datetime 使用详解的更多相关文章

  1. C# 内置 DateTime类详解

    C# 内置 DateTime类详解 摘抄自微软官方文档,用来方便自己查阅:网址:https://msdn.microsoft.com/zh-cn/library/system.datetime(v=v ...

  2. (转)python time模块和datetime模块详解

    python time模块和datetime模块详解 原文:http://www.cnblogs.com/tkqasn/p/6001134.html 一.time模块 time模块中时间表现的格式主要 ...

  3. datetime 模块详解 -- 基本的日期和时间类型

    转自:https://www.cnblogs.com/fclbky/articles/4098204.html datetime 模块提供了各种类用于操作日期和时间,该模块侧重于高效率的格式化输出 在 ...

  4. C#时间格式化(Datetime)用法详解

    Datetime.ToString(String, IFormatProvider) 参数format格式详细用法: 格式字符 关联属性/说明 d ShortDatePattern D LongDat ...

  5. python time模块和datetime模块详解

    一.time模块 time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.struct_time时间元组,共 ...

  6. Java 8 Date-Time API 详解

    从Java版本1.0开始就支持日期和时间,主要通过java.util.Date类. 但是,Date类设计不佳. 例如,Date中的月份从1开始,但从日期却从0开始.在JDK 1.1中使用它的许多方法已 ...

  7. python datetime模块详解

    datetime是python当中比较常用的时间模块,用于获取时间,时间类型之间转化等,下文介绍两个实用类. 一.datetime.datetime类: datetime.datetime.now() ...

  8. time&datetime模块详解

     一.time模块 1.时间格式转换图: 2.time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.for ...

  9. Python全栈之路----常用模块----datetime模块详解

    相比于time模块,datetime模块的接口则更直观,更容易调用. datetime模块定义了下面这几个类: datetime.date:表示日期的类,常用的属性有year,month,day: d ...

  10. day21 Pythonpython time模块和datetime模块详解

    一.time模块 time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.struct_time时间元组,共 ...

随机推荐

  1. 小白两篇博客熟练操作MySQL 之 第一篇

    小白两篇博客熟悉操作MySQL  之   第一篇 一.概述 1. 什么是数据库? 答: 储存数据的仓库, 如: 在ATM的事例中创建的一个db 目录, 称为数据库 2. 什么是Mysql, Oracl ...

  2. 20170704-WNDR4300uboot help info

    Unknown command 'env' - try 'help'ar7240> help? - alias for 'help'base - print or set address off ...

  3. vue组件 $children,$refs,$parent的使用详解

    1)$refs 首先你的给子组件做标记.demo :<firstchild ref="one"></firstchild> 然后在父组件中,通过this.$ ...

  4. JavaScript中的call()和apply()方法,借此实现继承

    1.方法定义 call方法: 语法:call([thisObj[,arg1[, arg2[,   [,.argN]]]]]) 定义:调用一个对象的一个方法,以另一个对象替换当前对象. 说明: call ...

  5. N天学习一个linux命令之ssh-keygen

    用途 生成ssh加密算法需要使用到的秘钥以及管理和转换 用法 ssh-keygen [-q] [-b bits] -t type [-N new_passphrase] [-C comment] [- ...

  6. MySQL的各种网络IO超时的用法和实现

    2016-04-06 赵伟 数据库开发者 客户端C API 在C API中调用mysql_options()来设置mysql_init() 所创建的连接对象的属性,使用这三个选项可以设置连接超时和读写 ...

  7. HDU 5016 Mart Master II (树上点分治)

    题目地址:pid=5016">HDU 5016 先两遍DFS预处理出每一个点距近期的基站的距离与基站的编号. 然后找重心.求出每一个点距重心的距离.然后依据dis[x]+dis[y] ...

  8. Android平台Airplay的实现方法

    Airplay属于局域网内异构设备之间分享多媒体数据的一种通信协议.Airplay设备有客户端和服务器之分,一般将小屏IOS设备实现为Airplay客户端,大屏幕设备实现为Airplay服务器.即iP ...

  9. hdu5119 Happy Matt Friends(dp)

    题目链接:点击打开链接 题目描写叙述:给出n个数.求从这n个数中随意取出一些数(至少取一个)相互异或之后大于m的方案数? 解题思路:分析因为n<=40&&m<=10^6,因 ...

  10. 数据结构(C实现)------- 顺序栈

    栈是限定仅在表的一端进行插入或删除的纯属表,通常称同意插入.删除的一端为栈顶(Top),对应在的.则称还有一端为栈底(Bottom). 不含元素的栈则称为空栈. 所设栈S={a1,a2,a3,..., ...