查找当前结算周期的结束

时间:2017-09-27 09:28:28

标签: c# datetime date-arithmetic

我已根据季度找到了答案,但需要将其延伸到其他结算周期。我已经走了一圈,无法找到出路!

我正在编写一个使用Dynamics 365(在线)的C#控制台应用程序,但我想这更像是一个数学问题。

我有一家公司未来有续订日,例如01/02/201(2月1日)。

新订单的结算周期可以是每月,每季度,每半年或每年。 我需要计算从续订日期开始向后工作的当前结算周期结束的日期。

这是我到目前为止所拥有的

DateTime contractRenewal = new DateTime(2018, 02, 01);
int MonthsInBillPeriod = getBillingPeriod() //returns 1 for monthly, 3 for quarterly etc.
int currQuarter = (DateTime.Now.Month - contractRenewal.Month) / MonthsInBillPeriod + 1;
int month = currQuarter * MonthsInBillPeriod + contractRenewal.Month;
while (month > 12)
    month = month - MonthsInBillPeriod;
renewal = new DateTime(DateTime.Now.AddMonths(month).Year, month, 1).AddDays(-1); //should return 30/09/2017 for monthly. 31/10/2017 for quarterly. 31/01/2018 for biannual and annual given contractRenewal date

1 个答案:

答案 0 :(得分:1)

如果没有考虑宿舍,你这样处理几个月是不够的?像这样:

DateTime tmpRenewal = contractRenewal;
while(tmpRenewal > DateTime.Now)
{
    tmpRenewal = tmpRenewal.AddMonths(-MonthsInBillPeriod);
}

// need to go one period forward, to the future from Now (as we are in the past right now)
DateTime renewal = tmpRenewal.AddMonths(MonthsInBillPeriod).AddDays(-1);