我正在尝试计算会计年度并在5-4-4日程安排的一周结束时返回日期。
例如会计年度从1月1日开始,然后我将返回日期5周,4周和另外4周。然后重复5周,4周,4周,直到会计年度结束,然后大概重新开始。
您将如何在数学上或程序上进行此操作?
我想出了一种减法方法,但是想看看是否有人有更好的解决方案:
52 - 47 = 5 | February 5th, 2018
52 - 43 = 9 | March 5th, 2018
52 - 39 = 13 | April 2nd, 2018
52 - 34 = 18 | May 7th, 2018
52 - 30 = 22 | June 4th, 2018
52 - 26 = 26 | July 3rd, 2018
52 - 21 = 31 | August 7th, 2018
52 - 17 = 35 | September 4th, 2018
52 - 13 = 39 | October 2nd, 2018
52 - 8 = 44 | November 6th, 2018
52 - 4 = 48 | December 4th, 2018
52 - 0 = 52 | January 1st, 2019
答案 0 :(得分:0)
最终自己解决了这个问题。在互联网上找不到任何内容。
解决方案: 我的算法最后使用减法运算,但是对我的问题进行了修改。
int[] pattern = {5, 4, 4}; // The week pattern
DateTime begin = new DateTime(2018, 1, 1);
DateTime currentDate = DateTime.Today; // 9-27-2018
// Get the total amount of weeks between the 2 days. Add 1 for including the end day
var currentWeek = (currentDate.Date.Subtract(beginTime).TotalDays + 1) / 7;
var counter = 0;
while (currentWeek > 0)
{
if (counter > 2)
{
counter = 0;
}
currentWeek = currentWeek - pattern[counter];
}
return currentWeek == 0;
然后,它返回一个布尔值,即当前日期是否为5-4-4日程安排中某个时间段的结束。在这种情况下,它将返回错误原因,因为9 / 27-2018并非财务日历末期的一天,其格式为5-4-4。