2017-09-27 76 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

謝謝,簡單解決了問題並解決了它! –