时间的操作System.DateTime
1 DateTime如何存储时间
2 如何在DateTime对象和字符串对象之间进行转换
3 什么是UTC时间,如何转换到UTC时间

时间的操作System.DateTime
1 DateTime如何存储时间

DateTime结果内部存储在一个64位长的整数,其中低62位来代表时间,它表示从0001年1月1日0时0分0秒开始到表示的时间经过百毫微秒数

2 如何在DateTime对象和字符串对象之间进行转换

DateTime可以通过ToString方法来格式化输出到字符串。

TryParse和Parse方法则用来从一个字符串中得到信息并构建一个DateTime对象。

3 什么是UTC时间,如何转换到UTC时间

UTC是格林威治标准时间(GMT),它是在0度经度测量到的时间。通过ToUniversalTime和ToLocal方法可以在本地时间和UTC时间之间进行转换。两个算法在转换时会相应地设置保存时间的64位整数的高两位标志,并且转换的算法考虑了夏令时。

对时间的一些操作:

  1. //当前时间now:2015/9/10 16:05:43
  2. now.GetDateTimeFormats('d')[] //2015/9/10
  3. now.GetDateTimeFormats('d')[] //2015-9-10
  4. now.GetDateTimeFormats('d')[] //2015.9.10
  5. now.GetDateTimeFormats('d')[] //2015-09-10
  6. now.GetDateTimeFormats('d')[] //2015/09/10
  7. now.GetDateTimeFormats('d')[] //2015.09.10
  8. now.GetDateTimeFormats('d')[] //15-9-10
  9. now.GetDateTimeFormats('d')[] //15/9/10
  10. now.GetDateTimeFormats('d')[] //15.9.10
  11. now.GetDateTimeFormats('d')[] //15/09/10
  12. now.GetDateTimeFormats('D')[] //2015年9月10日
  13. now.GetDateTimeFormats('D')[] //2015年9月10日,星期四
  14. now.GetDateTimeFormats('D')[] //星期四,2015年9月10日
  15. now.GetDateTimeFormats('f')[] //2015年9月10日 16:05
  16. now.GetDateTimeFormats('f')[] //2015年9月10日 16:05
  17. now.GetDateTimeFormats('f')[] //2015年9月10日 下午 4:05
  18. now.GetDateTimeFormats('f')[] //2015年9月10日 下午 04:05
  19. now.GetDateTimeFormats('f')[] //2015年9月10日,星期四 16:05
  20. now.GetDateTimeFormats('f')[] //2015年9月10日,星期四 16:05
  21. now.GetDateTimeFormats('f')[] //2015年9月10日,星期四 下午 4:05
  22. now.GetDateTimeFormats('f')[] //2015年9月10日,星期四 下午 04:05
  23. now.GetDateTimeFormats('f')[] //星期四,2015年9月10日 16:05
  24. now.GetDateTimeFormats('f')[] //星期四,2015年9月10日 16:05
  25. now.GetDateTimeFormats('f')[] //星期四,2015年9月10日 下午 4:05
  26. now.GetDateTimeFormats('f')[] //星期四,2015年9月10日 下午 04:05
  27. now.GetDateTimeFormats('F')[] //2015年9月10日 16:05:43
  28. now.GetDateTimeFormats('F')[] //2015年9月10日 16:05:43
  29. now.GetDateTimeFormats('F')[] //2015年9月10日 下午 4:05:43
  30. now.GetDateTimeFormats('F')[] //2015年9月10日 下午 04:05:43
  31. now.GetDateTimeFormats('F')[] //2015年9月10日,星期四 16:05:43
  32. now.GetDateTimeFormats('F')[] //2015年9月10日,星期四 16:05:43
  33. now.GetDateTimeFormats('F')[] //2015年9月10日,星期四 下午 4:05:43
  34. now.GetDateTimeFormats('F')[] //2015年9月10日,星期四 下午 04:05:43
  35. now.GetDateTimeFormats('F')[] //星期四,2015年9月10日 16:05:43
  36. now.GetDateTimeFormats('F')[] //星期四,2015年9月10日 16:05:43
  37. now.GetDateTimeFormats('F')[] //星期四,2015年9月10日 下午 4:05:43
  38. now.GetDateTimeFormats('F')[] //星期四,2015年9月10日 下午 04:05:43
  39. now.GetDateTimeFormats('g')[] //2015/9/10 16:05
  40. now.GetDateTimeFormats('g')[] //2015/9/10 16:05
  41. now.GetDateTimeFormats('g')[] //2015/9/10 下午 4:05
  42. now.GetDateTimeFormats('g')[] //2015/9/10 下午 04:05
  43. now.GetDateTimeFormats('g')[] //2015-9-10 16:05
  44. now.GetDateTimeFormats('g')[] //2015-9-10 16:05
  45. now.GetDateTimeFormats('g')[] //2015-9-10 下午 4:05
  46. now.GetDateTimeFormats('g')[] //2015-9-10 下午 04:05
  47. now.GetDateTimeFormats('g')[] //2015.9.10 16:05
  48. now.GetDateTimeFormats('g')[] //2015.9.10 16:05
  49. now.GetDateTimeFormats('g')[] //2015.9.10 下午 4:05
  50. now.GetDateTimeFormats('g')[] //2015.9.10 下午 04:05
  51. now.GetDateTimeFormats('g')[] //2015-09-10 16:05
  52. now.GetDateTimeFormats('g')[] //2015-09-10 16:05
  53. now.GetDateTimeFormats('g')[] //2015-09-10 下午 4:05
  54. now.GetDateTimeFormats('g')[] //2015-09-10 下午 04:05
  55. now.GetDateTimeFormats('g')[] //2015/09/10 16:05
  56. now.GetDateTimeFormats('g')[] //2015/09/10 16:05
  57. now.GetDateTimeFormats('g')[] //2015/09/10 下午 4:05
  58. now.GetDateTimeFormats('g')[] //2015/09/10 下午 04:05
  59. now.GetDateTimeFormats('g')[] //2015.09.10 16:05
  60. now.GetDateTimeFormats('g')[] //2015.09.10 16:05
  61. now.GetDateTimeFormats('g')[] //2015.09.10 下午 4:05
  62. now.GetDateTimeFormats('g')[] //2015.09.10 下午 04:05
  63. now.GetDateTimeFormats('g')[] //15-9-10 16:05
  64. now.GetDateTimeFormats('g')[] //15-9-10 16:05
  65. now.GetDateTimeFormats('g')[] //15-9-10 下午 4:05
  66. now.GetDateTimeFormats('g')[] //15-9-10 下午 04:05
  67. now.GetDateTimeFormats('g')[] //15/9/10 16:05
  68. now.GetDateTimeFormats('g')[] //15/9/10 16:05
  69. now.GetDateTimeFormats('g')[] //15/9/10 下午 4:05
  70. now.GetDateTimeFormats('g')[] //15/9/10 下午 04:05
  71. now.GetDateTimeFormats('g')[] //15.9.10 16:05
  72. now.GetDateTimeFormats('g')[] //15.9.10 16:05
  73. now.GetDateTimeFormats('g')[] //15.9.10 下午 4:05
  74. now.GetDateTimeFormats('g')[] //15.9.10 下午 04:05
  75. now.GetDateTimeFormats('g')[] //15/09/10 16:05
  76. now.GetDateTimeFormats('g')[] //15/09/10 16:05
  77. now.GetDateTimeFormats('g')[] //15/09/10 下午 4:05
  78. now.GetDateTimeFormats('g')[] //15/09/10 下午 04:05
  79. now.GetDateTimeFormats('G')[] //2015/9/10 16:05:43
  80. now.GetDateTimeFormats('G')[] //2015/9/10 16:05:43
  81. now.GetDateTimeFormats('G')[] //2015/9/10 下午 4:05:43
  82. now.GetDateTimeFormats('G')[] //2015/9/10 下午 04:05:43
  83. now.GetDateTimeFormats('G')[] //2015-9-10 16:05:43
  84. now.GetDateTimeFormats('G')[] //2015-9-10 16:05:43
  85. now.GetDateTimeFormats('G')[] //2015-9-10 下午 4:05:43
  86. now.GetDateTimeFormats('G')[] //2015-9-10 下午 04:05:43
  87. now.GetDateTimeFormats('G')[] //2015.9.10 16:05:43
  88. now.GetDateTimeFormats('G')[] //2015.9.10 16:05:43
  89. now.GetDateTimeFormats('G')[] //2015.9.10 下午 4:05:43
  90. now.GetDateTimeFormats('G')[] //2015.9.10 下午 04:05:43
  91. now.GetDateTimeFormats('G')[] //2015-09-10 16:05:43
  92. now.GetDateTimeFormats('G')[] //2015-09-10 16:05:43
  93. now.GetDateTimeFormats('G')[] //2015-09-10 下午 4:05:43
  94. now.GetDateTimeFormats('G')[] //2015-09-10 下午 04:05:43
  95. now.GetDateTimeFormats('G')[] //2015/09/10 16:05:43
  96. now.GetDateTimeFormats('G')[] //2015/09/10 16:05:43
  97. now.GetDateTimeFormats('G')[] //2015/09/10 下午 4:05:43
  98. now.GetDateTimeFormats('G')[] //2015/09/10 下午 04:05:43
  99. now.GetDateTimeFormats('G')[] //2015.09.10 16:05:43
  100. now.GetDateTimeFormats('G')[] //2015.09.10 16:05:43
  101. now.GetDateTimeFormats('G')[] //2015.09.10 下午 4:05:43
  102. now.GetDateTimeFormats('G')[] //2015.09.10 下午 04:05:43
  103. now.GetDateTimeFormats('G')[] //15-9-10 16:05:43
  104. now.GetDateTimeFormats('G')[] //15-9-10 16:05:43
  105. now.GetDateTimeFormats('G')[] //15-9-10 下午 4:05:43
  106. now.GetDateTimeFormats('G')[] //15-9-10 下午 04:05:43
  107. now.GetDateTimeFormats('G')[] //15/9/10 16:05:43
  108. now.GetDateTimeFormats('G')[] //15/9/10 16:05:43
  109. now.GetDateTimeFormats('G')[] //15/9/10 下午 4:05:43
  110. now.GetDateTimeFormats('G')[] //15/9/10 下午 04:05:43
  111. now.GetDateTimeFormats('G')[] //15.9.10 16:05:43
  112. now.GetDateTimeFormats('G')[] //15.9.10 16:05:43
  113. now.GetDateTimeFormats('G')[] //15.9.10 下午 4:05:43
  114. now.GetDateTimeFormats('G')[] //15.9.10 下午 04:05:43
  115. now.GetDateTimeFormats('G')[] //15/09/10 16:05:43
  116. now.GetDateTimeFormats('G')[] //15/09/10 16:05:43
  117. now.GetDateTimeFormats('G')[] //15/09/10 下午 4:05:43
  118. now.GetDateTimeFormats('G')[] //15/09/10 下午 04:05:43
  119. now.GetDateTimeFormats('M')[] //9月10日
  120. now.GetDateTimeFormats('m')[] //9月10日
  121. now.GetDateTimeFormats('O')[] //2015-09-10T16:05:43.6134074+08:00
  122. now.GetDateTimeFormats('o')[] //2015-09-10T16:05:43.6134074+08:00
  123. now.GetDateTimeFormats('R')[] //Thu, 10 Sep 2015 16:05:43 GMT
  124. now.GetDateTimeFormats('r')[] //Thu, 10 Sep 2015 16:05:43 GMT
  125. now.GetDateTimeFormats('s')[] //2015-09-10T16:05:43
  126. now.GetDateTimeFormats('t')[] //16:05
  127. now.GetDateTimeFormats('t')[] //16:05
  128. now.GetDateTimeFormats('t')[] //下午 4:05
  129. now.GetDateTimeFormats('t')[] //下午 04:05
  130. now.GetDateTimeFormats('T')[] //16:05:43
  131. now.GetDateTimeFormats('T')[] //16:05:43
  132. now.GetDateTimeFormats('T')[] //下午 4:05:43
  133. now.GetDateTimeFormats('T')[] //下午 04:05:43
  134. now.GetDateTimeFormats('u')[] //2015-09-10 16:05:43Z
  135. now.GetDateTimeFormats('U')[] //2015年9月10日 8:05:43
  136. now.GetDateTimeFormats('U')[] //2015年9月10日 08:05:43
  137. now.GetDateTimeFormats('U')[] //2015年9月10日 上午 8:05:43
  138. now.GetDateTimeFormats('U')[] //2015年9月10日 上午 08:05:43
  139. now.GetDateTimeFormats('U')[] //2015年9月10日,星期四 8:05:43
  140. now.GetDateTimeFormats('U')[] //2015年9月10日,星期四 08:05:43
  141. now.GetDateTimeFormats('U')[] //2015年9月10日,星期四 上午 8:05:43
  142. now.GetDateTimeFormats('U')[] //2015年9月10日,星期四 上午 08:05:43
  143. now.GetDateTimeFormats('U')[] //星期四,2015年9月10日 8:05:43
  144. now.GetDateTimeFormats('U')[] //星期四,2015年9月10日 08:05:43
  145. now.GetDateTimeFormats('U')[] //星期四,2015年9月10日 上午 8:05:43
  146. now.GetDateTimeFormats('U')[] //星期四,2015年9月10日 上午 08:05:43
  147. now.GetDateTimeFormats('Y')[] //2015年9月
  148. now.GetDateTimeFormats('Y')[] //2015.9
  149. now.GetDateTimeFormats('y')[] //2015年9月
  150. now.GetDateTimeFormats('y')[] //2015.9

GetDateTimeFormats(char)方法中参数有d, D, f, F, g, G, M (or m), O (or o), R (or r), s, t, T, u, U, and Y (or y)

这个方法实在是太难记住了,可以用DateTime.ToString(string)方法来格式化时间。

DateTime.ToString(string format)功能更强大,能输出不同格式的日期。

在这里列出了参数format格式详细用法(来自网络转载)
=======================
格式字符 关联属性/说明
d ShortDatePattern
D LongDatePattern
f 完整日期和时间(长日期和短时间)
F FullDateTimePattern(长日期和长时间)
g 常规(短日期和短时间)
G 常规(短日期和长时间)
m、M MonthDayPattern
r、R RFC1123Pattern
s 使用当地时间的 SortableDateTimePattern(基于 ISO 8601)
t ShortTimePattern
T LongTimePattern
u UniversalSortableDateTimePattern 用于显示通用时间的格式
U 使用通用时间的完整日期和时间(长日期和长时间)
y、Y YearMonthPattern

下表列出了可被合并以构造自定义模式的模式
=============================
这些模式是区分大小写的;例如,识别“MM”,但不识别“mm”。如果自定义模式包含空白字符或用单引号括起来的字符,则输出字符串页也将包含这些字符。未定义为格式模式的一部分或未定义为格式字符的字符按其原义复制。

格式模式 说明 :
d 月中的某一天。一位数的日期没有前导零。
dd 月中的某一天。一位数的日期有一个前导零。
ddd 周中某天的缩写名称,在 AbbreviatedDayNames 中定义。
dddd 周中某天的完整名称,在 DayNames 中定义。
M 月份数字。一位数的月份没有前导零。
MM 月份数字。一位数的月份有一个前导零。
MMM 月份的缩写名称,在 AbbreviatedMonthNames 中定义。
MMMM 月份的完整名称,在 MonthNames 中定义。
y 不包含纪元的年份。如果不包含纪元的年份小于 10,则显示不具有前导零的年份。
yy 不包含纪元的年份。如果不包含纪元的年份小于 10,则显示具有前导零的年份。
yyyy 包括纪元的四位数的年份。
gg 时期或纪元。如果要设置格式的日期不具有关联的时期或纪元字符串,则忽略该模式。
h 12 小时制的小时。一位数的小时数没有前导零。
hh 12 小时制的小时。一位数的小时数有前导零。
H 24 小时制的小时。一位数的小时数没有前导零。
HH 24 小时制的小时。一位数的小时数有前导零。
m 分钟。一位数的分钟数没有前导零。
mm 分钟。一位数的分钟数有一个前导零。
s 秒。一位数的秒数没有前导零。
ss 秒。一位数的秒数有一个前导零。
f 秒的小数精度为一位。其余数字被截断。
ff 秒的小数精度为两位。其余数字被截断。
fff 秒的小数精度为三位。其余数字被截断。
ffff 秒的小数精度为四位。其余数字被截断。
fffff 秒的小数精度为五位。其余数字被截断。
ffffff 秒的小数精度为六位。其余数字被截断。
fffffff 秒的小数精度为七位。其余数字被截断。
t 在 AMDesignator 或 PMDesignator 中定义的 AM/PM 指示项的第一个字符(如果存在)。
tt 在 AMDesignator 或 PMDesignator 中定义的 AM/PM 指示项(如果存在)。
z 时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数没有前导零。例如,太平洋标准时间是“-8”。
zz 时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数有前导零。例如,太平洋标准时间是“-08”。
zzz 完整时区偏移量(“+”或“-”后面跟有小时和分钟)。一位数的小时数和分钟数有前导零。例如,太平洋标准时间是“-08:00”。
: 在 TimeSeparator 中定义的默认时间分隔符。
/ 在 DateSeparator 中定义的默认日期分隔符。
% c 其中 c 是格式模式(如果单独使用)。如果格式模式与原义字符或其他格式模式合并,则可以省略“%”字符。
/ c 其中 c 是任意字符。照原义显示字符。若要显示反斜杠字符,请使用“//”。

只有上面第二个表中列出的格式模式才能用于创建自定义模式;在第一个表中列出的标准格式字符不能用于创建自定义模式。自定义模式的长度至少为两个字符;例如,

DateTime.ToString( "d") 返回 DateTime 值;“d”是标准短日期模式。
DateTime.ToString( "%d") 返回月中的某天;“%d”是自定义模式。
DateTime.ToString( "d ") 返回后面跟有一个空白字符的月中的某天;“d”是自定义模式。

比较方便的是,上面的参数可以随意组合,并且不会出错,多试试,肯定会找到你要的时间格式

转载请注明出处:

作者:JesseLZJ
出处:http://jesselzj.cnblogs.com

.NET基础 (12)时间的操作System.DateTime的更多相关文章

  1. android基础篇------------java基础(12)(多线程操作)

    <一>基本概念理解 1.什么是进程? 进程就是在某种程度上相互隔离,独立运行的程序.一般来说,系统都是支持多进程操作的,这所谓的多进程就是让系统好像同时运行多个程序. 2.什么是线程呢? ...

  2. C语言基础 (12) 文件的操作 FILE

    课程回顾 结构体基本操作: 结构体类型的定义 // struct为关键字 Stu为自定义标识符 // struct Stu才是结构体类型 // 结构体成员不能在定义类型时赋值 struct Stu { ...

  3. 『心善渊』Selenium3.0基础 — 12、Selenium操作鼠标和键盘事件

    目录 (一)对鼠标的操作 1.鼠标事件介绍 2.ActionChains 类鼠标操作的常用方法 3.perform()方法 4.鼠标事件操作步骤 5.示例 (1)右键单击.左键双击 (2)鼠标拖拽动作 ...

  4. System.DateTime.cs

    ylbtech-System.DateTime.cs 1. 程序集 mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c5 ...

  5. Python3 与 C# 面向对象之~继承与多态 Python3 与 C# 面向对象之~封装 Python3 与 NetCore 基础语法对比(Function专栏) [C#]C#时间日期操作 [C#]C#中字符串的操作 [ASP.NET]NTKO插件使用常见问题 我对C#的认知。

    Python3 与 C# 面向对象之-继承与多态   文章汇总:https://www.cnblogs.com/dotnetcrazy/p/9160514.html 目录: 2.继承 ¶ 2.1.单继 ...

  6. Python基础 | 日期时间操作

    目录 获取时间 时间映射 格式转换 字符串转日期 日期转字符串 unixtime 时间计算 时间偏移 时间差 "日期时间数据"作为三大基础数据类型之一,在数据分析中会经常遇到. 本 ...

  7. Python时间与日期操作(datetime、time、calendar)

    相关模块 模块 说明 time time是一个仅包含与日期和时间相关的函数和常量的模块,在本模块中定义了C/C++编写的几个类.例如,struct_time类 datetime datetime是一个 ...

  8. System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") 显示24小时制;System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")显示12小时制

    this.Label6.Text = "当前时间:" + System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") ...

  9. C#中获取当前时间:System.DateTime.Now.ToString()用法

    //2008年4月24日 System.DateTime.Now.ToString("D"); //2008-4-24 System.DateTime.Now.ToString(& ...

随机推荐

  1. .NET可变性解析(协变和逆变)

    [一]何为可变性 可变性是.NET4.0中的一个新特性,可变性可分为 : 协变性.逆变性.不可变性. 那么在.NET4.0之前是否有可变性? 答案是肯定的,我们可以通过下面的几个实例来简单的了解一下. ...

  2. Hadoop序列化机制及实例

    序列化 1.什么是序列化?将结构化对象转换成字节流以便于进行网络传输或写入持久存储的过程.2.什么是反序列化?将字节流转换为一系列结构化对象的过程.序列化用途: 1.作为一种持久化格式. 2.作为一种 ...

  3. 关于 ImageLoader 说的够细了。。。

    简介ImageLoader(一) 分类: android 开源及第三方项目2014-05-30 12:14 14126人阅读 评论(0) 收藏 举报 ImageLoader 使用该开源项目的之前,先给 ...

  4. Lucene 中的Tokenizer, TokenFilter学习

      lucene中的TokenStream,TokenFilter之间关系   TokenStream是一个能够在被调用后产生语汇单元序列的类,其中有两个类型:Tokenizer和TokenFilte ...

  5. 停止调试 IIS 不退出

    在VS主面板打开:工具->选项->调试->编辑继续   取消选中[启用"编辑并继续"] 就可以(不过这是针对所有的调试). 若只想对单个项目进行设置,可以选择自己 ...

  6. python requests 爬取数据

    import requests from lxml import etree import time import pymysql import json headers={ 'User-Agent' ...

  7. three3D地图

    终于闲下来了,前段时间做了个项目,高精度精准定位系统,用到了three3D地图 听起来是不是很炫酷?其实并不难.先把部分代码附上(文件比较多,只粘贴部分的) $(function () {     i ...

  8. MySQL GTID (四)

    七. GTID的限制以及解决方案 7.1 事务中混合多个存储引擎,会产生多个GTID. 当使用GTID,在同一个事务中,更新包括了非事务引擎(MyISAM)和事务引擎(InnoDB)表的操作,就会导致 ...

  9. java.lang.NoClassDefFoundError: weblogic/kernel/KernelStatus

    solution Step: 1.In the classpath tab, be sure to add the wlclient.jar file located here \wlserver_1 ...

  10. C# 之 日常问题积累

    https://www.cnblogs.com/xinaixia/p/3956349.html