0

我正在尝试计算财政年度并返回 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
4

1 回答 1

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 模式的会计日历的结束期间的一天。

于 2018-09-27T21:02:27.163 回答