根据我所在的月份,我最简单的方法是分配我的季度结束月份?
有什么我可以像模数一样使用?我想要最简单的情况和最少量的代码行
我的季度结束月份是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);
那这个呢?
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);
}
最少量的行可能是这样的:
var d = new DateTime(DateTime.Now.Year, 3*(1+(mymonth-1)/3), 15);
但那是丑陋的地狱:)