-1
$\begingroup$

I have a system in which I need to be able to calculate the week number and day number from any Monday to the current date. Weeks can only be 1-4, then it starts over. So, if we are 5 weeks from the "Monday start date", then we're actually on Week #1 again. Days can only be 0-6. 1 = Monday, etc. Start date must always be a Monday, but it can be any Monday of any month, any year, etc.

Today is Friday, March 22, 2024. If my start date is Monday, March 18, 2024, then the expected outcome is this:

Week=1
Day=5 (because today is Friday)

If the start date is Monday, December 4, 2023, then:

Week=4
Day=5 (because today is Friday)

So I built a function to calculate all this, but when the start date is more than 4 weeks back, the calculation is wrong, and for obvious reasons. Consider:

The function DateTest() has 3 input params:

  1. Start Date
  2. Expected week #
  3. Expected day #

Notes:

  1. All date calcs are relative to the start date (passed into the function) and today's date (Friday, March 22, 2024).
  2. I calculated params 2 and 3 (expected week # and expected day #) by looking at a calendar and brute-force coming up with the right answer.

Here are the function calls:

DateTest("2023-12-04", 4, 5);
DateTest("2023-12-11", 3, 5);
DateTest("2023-12-18", 2, 5);
DateTest("2023-12-25", 1, 5);
DateTest("2024-01-01", 4, 5);
DateTest("2024-01-08", 3, 5);
DateTest("2024-01-15", 2, 5);
DateTest("2024-01-22", 1, 5);
DateTest("2024-01-29", 4, 5);
DateTest("2024-02-05", 3, 5);
DateTest("2024-02-12", 2, 5);
DateTest("2024-02-19", 1, 5);
DateTest("2024-02-26", 4, 5);
DateTest("2024-03-04", 3, 5);
DateTest("2024-03-11", 2, 5);
DateTest("2024-03-18", 1, 5);

And here are the results from the above function calls, all aligned in sequence:

2023-12-04 - Week - Actual: 16, Expected: 4, Diff: 12
2023-12-11 - Week - Actual: 15, Expected: 3, Diff: 12
2023-12-18 - Week - Actual: 14, Expected: 2, Diff: 12
2023-12-25 - Week - Actual: 13, Expected: 1, Diff: 12
2024-01-01 - Week - Actual: 12, Expected: 4, Diff: 8
2024-01-08 - Week - Actual: 11, Expected: 3, Diff: 8
2024-01-15 - Week - Actual: 10, Expected: 2, Diff: 8
2024-01-22 - Week - Actual: 9, Expected: 1, Diff: 8
2024-01-29 - Week - Actual: 8, Expected: 4, Diff: 4
2024-02-05 - Week - Actual: 7, Expected: 3, Diff: 4
2024-02-12 - Week - Actual: 6, Expected: 2, Diff: 4
2024-02-19 - Week - Actual: 5, Expected: 1, Diff: 4
2024-02-26 - SUCCESS
2024-03-04 - SUCCESS
2024-03-11 - SUCCESS
2024-03-18 - SUCCESS

Note: When the week and day match, it simply says SUCCESS. I am only showing WEEK data because the day # matches in all cases.

When you look at the results, you will immediately see that expected week # vs. actual week # brings a simple and obvious difference pattern. I need to perform a bit of math to make the week number always correct. The problem is that I can't make my brain come up with the equation required.

I suspect the equation is a simple division followed by a simple subtraction, but it's breaking my brain trying to come up with it. Please help. Thanks in advance.

$\endgroup$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.