Time formatting: Calendar week and date of first and last day of next week

Hi, I need help for formatting dates in the following pattern:

  1. KW (7.–13. Februar 2022)

Means:
Number of the next week, some suffix (KW), (date of the monday of next week - date of sunday next week year)

Februar is German for February.

Help is appreciated, I failed adjusting %ICUDateTime%w% to my needs.

Thanks,
Chris

Here’s what I came up with, but it comes with some caveats.

Next Week.kmmacros (3.7 KB)

Click to show image of macro

(If you download the macro, it will be in a group named “Temporary” and will be enabled.)

The key is to figure out how many days there are until the next Monday. To do that, calculate 2 - DOW() (Monday is Day of Week 2 in Keyboard Maestro) and add 7 if the result is less than or equal to 0. You then add 6 to that number to get the Sunday after the next Monday.

Once you have that worked out, it’s just a tedious set of calls to the %ICUDateTimePlus% token with different formatting codes:

%ICUDateTimePlus%InstanceDaysToNextMonday%Days%w. 'KW ('d.%–%ICUDateTimePlus%InstanceDaysToNextMonday+6%Days%d. MMMM yyyy)%

The single quote marks in the first token are a way of “escaping” the text inside so it doesn’t get interpreted as a formatting string.

Now for the caveats:

  • I’m in the US, so my month name comes out as “February.” If your computer is set up with a German locale, I think it should come out as “Februar,” but I don’t have a way of testing that.
  • Week numbers can be very tricky and can also depend on locale. On my computer, next week is Week 7, not Week 6.
  • You may need to think more about your formatting string. It’s fine to say “7.–13. Februar 2022” when the entire week is within a single month, but what if the week starts in one month and ends in another? Is “28.–6. März 2022” really what you want? The same thing can happen when a week starts in one year and ends in the next.
  • If the “KW” part can change, you'll have to update the logic to allow for different suffixes.

I doubt my macro will be everything you need, but it should get you started on the right path.

1 Like

Thank you very much for your outstandig effort. I try to wrap my mind around it and adapt it to my needs. These %ICUDateTimePlus% tokens can be confusing at times.

Thanks again!