2017-12-18 10 views
-1

今月の月を基準に四半期終了月を割り当てる最も簡単な方法は何ですか?四半期終了日

モジュロのようなものがありますか?私は、このようなロジックを実行しないようにする最も簡単なケースと私の四半期ヶ月ヶ月3,6,9および12

ある終了コード

のラインの最小量をしたい:

if (1 <= mymonth && mymonth <= 3) 
    mymonth = new DateTime(DateTime.Now.Year, 3, 15); 
else if (4 <= mymonth && mymonth <= 6) 
    mymonth = new DateTime(DateTime.Now.Year, 6, 15); 
else if (7 <= mymonth && mymonth <= 9) 
    mymonth = new DateTime(DateTime.Now.Year, 9, 15); 
else 
    mymonth = new DateTime(DateTime.Now.Year, 12, 15); 
+1

https://stackoverflow.com/q/1492079/1070452 – Plutonix

答えて

0

これはどうですか?

を使用でき
public DateTime GetQuarterEnd(int month) 
{ 
    if (month < 1 || month > 12) 
     throw new ArgumentException("month is not a valid Month of the year."); 

    var mod = month % 3; 
    var actual = month/3 + (mod > 0 ? 1 : 0); 
    return new DateTime(DateTime.Now.Year, actual * 3, 15); 
} 
0

最低額は次のようなものが考えられます。

var d = new DateTime(DateTime.Now.Year, 3*(1+(mymonth-1)/3), 15); 

しかし、それは地獄:)

として醜いです

https://dotnetfiddle.net/HZfFLF

関連する問題