csharp 下个月更高的月份
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 下个月更高的月份相关的知识,希望对你有一定的参考价值。
<span style="color: #008080; font-style: italic;">/// <summary></span>
<span style="color: #008080; font-style: italic;">/// Returns the next month day after the given DateTime.</span>
<span style="color: #008080; font-style: italic;">/// </summary></span>
<span style="color: #008080; font-style: italic;">/// <param name="T">Source DateTime</param></span>
<span style="color: #008080; font-style: italic;">/// <param name="D">Target month day</param></span>
<span style="color: #008080; font-style: italic;">/// <returns>DateTime</returns></span>
<span style="color: #0600FF;">public</span> DateTime NextHigherMonthDay<span style="color: #000000;">(</span>DateTime T, <span style="color: #FF0000;">int</span> D<span style="color: #000000;">)</span> <span style="color: #000000;">{</span>
<span style="color: #0600FF;">return</span> <span style="color: #000000;">(</span><span style="color: #000000;">(</span>T.<span style="color: #0000FF;">Day</span> >= D<span style="color: #000000;">)</span> ? T.<span style="color: #0000FF;">AddMonths</span><span style="color: #000000;">(</span><span style="color: #FF0000;">1</span><span style="color: #000000;">)</span> : T<span style="color: #000000;">)</span>.<span style="color: #0000FF;">AddDays</span><span style="color: #000000;">(</span>D-T.<span style="color: #0000FF;">Day</span><span style="color: #000000;">)</span>;
<span style="color: #000000;">}</span>
<span style="color: #008080; font-style: italic;">// With a minor change, you can create related functions like</span>
<span style="color: #008080; font-style: italic;">// the one below. I just changed the ">=" to ">" ...</span>
<span style="color: #0600FF;">public</span> DateTime NextHigherOrEqualMonthDay<span style="color: #000000;">(</span>DateTime T, <span style="color: #FF0000;">int</span> D<span style="color: #000000;">)</span>
<span style="color: #000000;">{</span>
<span style="color: #0600FF;">return</span> <span style="color: #000000;">(</span><span style="color: #000000;">(</span>T.<span style="color: #0000FF;">Day</span> > D<span style="color: #000000;">)</span> ? T.<span style="color: #0000FF;">AddMonths</span><span style="color: #000000;">(</span><span style="color: #FF0000;">1</span><span style="color: #000000;">)</span> : T<span style="color: #000000;">)</span>.<span style="color: #0000FF;">AddDays</span><span style="color: #000000;">(</span>D - T.<span style="color: #0000FF;">Day</span><span style="color: #000000;">)</span>;
<span style="color: #000000;">}</span>
以上是关于csharp 下个月更高的月份的主要内容,如果未能解决你的问题,请参考以下文章