2017-09-27 116 views
0

我找到了基于宿舍的答案,但需要将其扩展到其他结算周期。我一直在圈子里,找不到出路!查找当前结算周期的结尾

我正在写一个C#控制台应用程序与Dynamics 365(在线)工作,但我想这是一个数学问题。

我有一家公司在未来有一个续约日,例如, 01/02/2018(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

是不是足够为您办理个月,例如,在不考虑宿舍?像这样:

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

谢谢,简单解决了问题并解决了它! –