I am trying calculate the fiscal year and return the date when it is the end of a week on the 5-4-4 schedule.
E.g. Fiscal year starts January 1st then I will return the date 5 weeks, 4 weeks, and another 4 weeks. Then repeat 5 weeks, 4 weeks, 4 weeks until the end of the fiscal year then presumably restart it over again.
How would you mathematically or programmatically go about doing this?
I came up with a subtraction method, but wanted to see if anyone had a better solution:
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
Ended up solving the issue myself. Could not find anything on the internet.
Solution: My algorithm ended up using subtraction in the end but in a modified way from my question.
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;
This then returns a bool on whether the current date is the end of a time period in the 5-4-4 schedule. In this case it would return false cause 9-27-2018 is not a day at the end period on the fiscal calendar with a 5-4-4 pattern.