你真的清楚DateTime in C#吗?
<div id="output_wrapper_id" class="output_wrapper" style="font-size: 16px; color: rgba(62, 62, 62, 1); line-height: 1.6; word-spacing: 0; letter-spacing: 0; font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif"><blockquote style="line-height: inherit; display: block; padding: 15px 15px 15px 1rem; font-size: 0.9em; margin: 1em 0; color: rgba(129, 145, 152, 1); border-left: 6px solid rgba(220, 230, 240, 1); background-color: rgba(242, 247, 251, 1); overflow: auto; word-wrap: normal; word-break: normal">
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 0">DateTime,就是一个世界的大融合。</p>
</blockquote>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">日期和时间,在我们开发中非常重要。DateTime在C#中,专门用来表达和处理日期和时间。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">本文算是多年使用DateTime的一个总结,包括DateTime对象的整体应用,以及如何处理不同的区域、时区、格式等内容。</p>
<h1 id="hdatetime" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.6em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">一、什么是DateTime</span></h1>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">跟我们想的不一样,DateTime不是一个类(class),而是一个结构(struct),它存在于<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">System</code>命名空间下,在Dotnet Core中,处于<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">System.Runtime.dll</code>中。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">看一下DateTime的定义:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">struct</span> <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">DateTime</span> :</span> IComparable, IComparable<DateTime>, IConvertible, IEquatable<DateTime>, IFormattable, System.Runtime.Serialization.ISerializable<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">从定义可以知道,DateTime实现了<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">IComparable</code>、<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">IConvertible</code>、<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">IEquatable</code>、<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">IFormattable</code>、<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">ISerializable</code>。因此,DateTime可以让我们使用有关日期和时间的很多相关信息。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"><em style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; font-style: italic"> <span style="font-size: small; color: inherit; line-height: inherit; margin: 0; padding: 0">为了防止不提供原网址的转载,特在这里加上原文链接:https://www.cnblogs.com/tiger-wang/p/13303360.html</span></em></p>
<h1 id="h" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.6em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">二、构造</span></h1>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">初始化一个DateTime对象,C#提供了11种方式进行初始化,根据需要,可以使用年月日时分秒,以及<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">Ticks</code>。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"><code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">Ticks</code>是C#里一个独特的定义,它是以公历0001年1月1日00:00:00.000以来所经历的以100纳秒为间隔的间隔数。我们知道,纳秒、微秒、毫秒和秒之间都是1000倍的关系,所以,1毫秒等于10000Ticks。这个数字很重要。在C#到Javascript时间转换时,需要很清楚这个对应关系。</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">DateTime date1 = <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">new</span> DateTime(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">2020</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">7</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">14</span>);<br>DateTime date2 = <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">new</span> DateTime(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">2020</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">7</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">14</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">14</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">23</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">40</span>);<br>DateTime date3 = <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">new</span> DateTime(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">637303334200000000</span>);<br></code></pre>
<h1 id="h-1" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.6em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">三、静态字段</span></h1>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">DateTime包括三个静态字段:</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">MinValue - DateTime的最小值,对应公历0001年1月1日00:00:00.000,Ticks为0;</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">MaxValue - DateTime的最大值,对应公历9999看12月31日23:59:59.999,Ticks为3155378975999999999;</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">UnixEpoch - Unix、Javascript下的时间起点,对应公历1970年1月1日00:00:00.000,Ticks为621355968000000000;</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">在Javascript中,时间保存的是从UnixEpoch开始,即从1970年1月1日00:00:00.000开始到现在的毫秒数。所以,C#时间到Javascript时间的转换,可以用以下代码:</p>
<pre><code class="hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">static</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">long</span> <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">ToUnixTicks</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 152, 35, 1); word-wrap: inherit !important; word-break: inherit !important">(<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">this</span> DateTime time)</span><br></span>{<br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">return</span> (<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">long</span>)TimeSpan.FromTicks(time.Ticks - DateTime.UnixEpoch.Ticks).TotalMilliseconds - TimeZoneInfo.Local.GetUtcOffset(time).Hours * <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">60</span> * <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">60</span> * <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">1000</span>;<br>}<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">在Javascript中引入时间:</p>
<pre><code class="javascript language-javascript hljs" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">var</span> time = <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">new</span> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">Date</span>().setTime(unix_ticks);<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">就完成了转换。</p>
<h1 id="h-2" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.6em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">四、方法</span></h1>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">DateTime提供了很多种方法来操作DateTime对象,用于处理诸如向日期添加天数、小时、分钟、秒、日期差异、从字符串解析到datetime对象、获得通用时间等等。这儿就不详细说了,需要了可以查微软文档,很详细。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">给几个例子:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">TimeSpan duration = <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">new</span> System.TimeSpan(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">30</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">0</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">0</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">0</span>);<br>DateTime newDate1 = DateTime.Now.Add(duration);<br><br>DateTime today = DateTime.Now;<br>DateTime newDate2 = today.AddDays(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">30</span>);<br><br><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">string</span> dateString = <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"2020-07-14 14:23:40"</span>;<br>DateTime dateTime12 = DateTime.Parse(dateString);<br><br>DateTime date1 = <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">new</span> System.DateTime(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">2020</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">7</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">13</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">14</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">20</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">10</span>);<br>DateTime date2 = <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">new</span> System.DateTime(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">2020</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">7</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">14</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">14</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">25</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">40</span>);<br>DateTime date3 = <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">new</span> System.DateTime(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">2020</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">7</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">14</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">14</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">25</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">40</span>);<br><br>TimeSpan diff1 = date2.Subtract(date1);<br>DateTime date4 = date3.Subtract(diff1);<br>TimeSpan diff2 = date3 - date2;<br><br>DateTime date5 = date2 - diff1;<br></code></pre>
<h1 id="h-3" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.6em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">五、属性</span></h1>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">DateTime提供了年月日时分秒、以及其它一些属性,用来方便提取日期中的细节。</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">DateTime myDate = <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">new</span> DateTime(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">2020</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">7</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">14</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">14</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">23</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">40</span>);<br><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">int</span> year = myDate.Year; <br><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">int</span> month = myDate.Month;<br><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">int</span> day = myDate.Day;<br><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">int</span> hour = myDate.Hour;<br><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">int</span> minute = myDate.Minute;<br><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">int</span> second = myDate.Second;<br><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">int</span> weekDay = (<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">int</span>)myDate.DayOfWeek;<br><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">string</span> weekString = myDate.DayOfWeek.ToString();<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">其中,DayOfWeek,是用来判断日期是星期几的,它是一个枚举值。注意,按照惯例,一周是从周日开始的,所以,0表示周日,6表示周六。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"><strong style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; font-weight: bold">DateTimeKind</strong>,用来定义实例表示的时间是基于本地时间(LocalTime)、UTC时间(UTC)或是不指定(Unspecified)。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">在大多数情况下,我们定义时间就直接定义年月日时分秒,例如下面:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">DateTime myDate = <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">new</span> DateTime(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">2020</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">7</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">14</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">14</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">23</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">40</span>);<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">这种定义下,这个时间就是<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">Unspecified</code>的。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">在使用时,如果应用过程中不做时间转换,始终以这种方式用,那不会有任何问题。但在某些情况下,时间有可能会发生转换,例如跨国应用的时间处理,再例如MongoDB,在数据库保存数据时,强制使用UTC时间。这种情况下,处理时间就必须采用<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">LocalTime</code>或<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">UTC</code>时间:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">DateTime myDate = <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">new</span> DateTime(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">2020</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">7</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">14</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">14</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">23</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">40</span>, DateTimeKind.Local);<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">或</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">DateTime myDate = <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">new</span> DateTime(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">2020</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">7</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">14</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">14</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">23</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">40</span>, DateTimeKind.Unspecified);<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">否则,在时间类型不确定的情况下,时间转换会出现问题。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">看看下面的例子:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">DateTime myDate = <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">new</span> DateTime(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">2020</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">7</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">14</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">14</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">23</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">40</span>);<br><br>var date1 = myDate.ToLocalTime();<br>Console.WriteLine(date1.ToString());<br><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">/* 7/14/2020 22:23:40 PM */</span><br><br>var date2 = myDate.ToUniversalTime();<br>Console.WriteLine(date2.ToString());<br><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">/* 7/14/2020 6:23:40 AM */</span><br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">当使用<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">ToLocalTime</code>方法时,<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">Unspecified</code>时间会认为自己是<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">UTC</code>时间,而当使用<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">ToUniversalTime</code>时,<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">Unspecified</code>时间又会认为自己是<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">LocalTime</code>时间,导致时间上的转换错误。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"><em style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; font-style: italic">关于MongoDB处理时间的相关内容,可以去看我的另一个文章:MongoDB via Dotnet Core数据映射详解</em></p>
<h1 id="h-4" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.6em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">六、时间对象的加减及比较</span></h1>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">DateTime时间对象的加减及比较非常方便。看例子:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">DateTime date1 = <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">new</span> System.DateTime(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">2020</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">7</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">14</span>);<br><br>TimeSpan timeSpan = <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">new</span> System.TimeSpan(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">10</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">5</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">5</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">1</span>);<br>DateTime addResult = date1 + timeSpan;<br>DateTime substarctResult = date1 - timeSpan; <br><br>DateTime date2 = <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">new</span> DateTime(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">2020</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">7</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">14</span>);<br>DateTime date3 = <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">new</span> DateTime(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">2020</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">7</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">15</span>);<br><br><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">bool</span> isEqual = date2 == date3;<br></code></pre>
<h1 id="h-5" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.6em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">七、日期的格式化</span></h1>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">日期的格式化是相关DateTime网上询问和查找最多的内容。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">有这么一个表:</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"><img src="https://img2020.cnblogs.com/blog/907112/202007/907112-20200716161815839-1298783290.png"></p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">对照这个表就可以:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">date.ToString(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"yyyy-MM-dd HH:mm:ss"</span>);<br></code></pre>
<h1 id="h-6" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.6em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">八、阴历</span></h1>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">DateTime本身依赖于日历Calendar类。Calendar是一个抽象类,在<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">System.Globalization</code>命名空间下,也在<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">System.Runtime.dll</code>中。而在Calendar类下面,提供了很多不同类型的日历。跟我们有关系的,是中国的阴历<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">ChineseLunisolarCalendar</code>。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">使用也很简单:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">Calendar calendar = <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">new</span> ChineseLunisolarCalendar();<br><br>DateTime date = <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">new</span> DateTime(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">2020</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">06</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">24</span>, calendar);<br><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">/* 7/14/2020 00:00:00 AM */</span><br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">嗯嗯,经常看阴历的伙伴们会看出一点问题:今天是阴历5月24,为什么这儿写的是6月24呢?这个是因为今天闰4月,所以,阴历5月实际是这一个阴历年的第6个月。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">那如何判断哪个月是否闰月呢?</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">Calendar calendar = <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">new</span> ChineseLunisolarCalendar();<br><br><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">bool</span> is_leapYear = calendar.IsLeapYear(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">2020</span>);<br><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">bool</span> is_leapMonth = calendar.IsLeapMonth(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">2020</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">5</span>);<br><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">bool</span> is_leapDay = calendar.IsLeapDay(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">2020</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">5</span>, <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">26</span>);<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">同样,我们也可以用公历转阴历:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">DateTime date = DateTime.Now;<br><br>Calendar calendar = <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">new</span> ChineseLunisolarCalendar();<br><br><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">int</span> year = calendar.GetYear(date);<br><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">/* 2020 */</span><br><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">int</span> month = calendar.GetMonth(date);<br><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">/* 6 */</span><br><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">int</span> day = calendar.GetDayOfMonth(date);<br><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">/* 24 */</span><br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">以上就是全部内容了。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">有没有发现,微软实现的功能,比我们想像的要多?</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">(全文完)</p>
<p> </p>
<hr>
<p> </p>
<table border="0">
<tbody>
<tr>
<td><img src="https://img2020.cnblogs.com/blog/907112/202005/907112-20200527230728396-985375280.jpg"></td>
<td>
<p>微信公众号:老王Plus</p>
<p>扫描二维码,关注个人公众号,可以第一时间得到最新的个人文章和内容推送</p>
<p>本文版权归作者所有,转载请保留此声明和原文链接</p>
</td>
</tr>
</tbody>
</table>
</div><br><br>
来源:https://www.cnblogs.com/tiger-wang/p/13303360.html
頁:
[1]