查找当前计费周期的结束时间

Find End of Current Billing Period

我找到了基于季度的答案,但需要将其扩展到其他计费周期。我一直在兜圈子,找不到出路!

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

我有一家公司在未来设置了续订日,例如2018 年 1 月 2 日(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

不考虑季度,你这样处理几个月还不够吗?像这样:

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);