我正在试图计算财政年度,并返回日期,当它是一个星期的结束,在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发布于 2018-09-27 21:02:27
最后我自己解决了这个问题。在网上找不到任何东西。
解决方案:我的算法最终使用减法,但修改了我的问题。
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计划中某个时间段的结束的bool。在这种情况下,它将返回虚假的原因9-27-2018年不是一天在财政日历的期末以5-4-4模式。
https://stackoverflow.com/questions/52489588
复制相似问题