The above version seems to work accurately now, and it is very fast for me.
So I see no reason to further optimize it.
But some coders prefer a minimalist approach, using the fewest possible lines of code or number of Actions.
For those who prefer this, I have created a "minimal" version (below).
Personally, I prefer the "full", albeit more verbose, version, because:
It is easier to read and understand, especially by those who did not author it.
It is easier to maintain, to debug, to modify.
To each his own.
##Macro Library@DATE Get Date for Prior/Next Day of Week [Minimal] [Example]
####DOWNLOAD:
<a class="attachment" href="/uploads/default/original/2X/4/4da87eb7fc44af27d293c46a906d6576c2b6b891.kmmacros">@DATE Get Date for Prior-Next Day of Week [Minimal] [Example].kmmacros</a> (12 KB)
---
<img src="/uploads/default/original/2X/9/934ef5ff8e6753d13901032fba1d1924a96531c1.png" width="759" height="763">
That only gives you the month-day-year. I don't know what else is going on, but the "Monday" is not coming from that token. If you want the DOW, you would do:
%ICUDateTimePlus%(7+5-DOW(NOW)) MOD 1%Days%EEEE MM-d-yy%
Here is another option. The GNU coreutils date command is a very feature-rich version of the [basic] macOS date command line utility, including an option for a âmostly free format human readable date stringâ.
Some examples
what time would it be locally (to me in the Mountain Standard timezone) when it is 9am in Istanbul this next Friday. $ gdate --date='TZ="Asia/Istanbul" 9:00 next Friday' Thu Jan 5 23:00:00 MST 2017
The UTC/GMT time when it is noon in Hawaii $ gdate --date='TZ="Pacific/Honolulu" 12:00' -u Wed Jan 4 22:00:00 UTC 2017
but to answer your original question $ gdate --date='next Sunday' "+%Y-%m-%d" 2017-01-08 $ gdate --date='last Sunday' "+%Y-%m-%d" 2017-01-01
Here are more examples of the âmostly free format human readable date stringâ $ gdate --date='3 weeks ago last Friday' "+%Y-%m-%d" 2016-12-09 $ gdate --date='2 weeks next Tuesday' "+%Y-%m-%d" 2017-01-24
and an all-time favorite $ gdate --date='a fortnight ago' "+%Y-%m-%d" 2016-12-20
Tip: Donât even try to download the source code and compile coreutils yourself. The easiest way is to install the Homebrew package manager (just a single line ruby command) then simply install coreutils with brew install coreutils.
Note: Since the coreutils executable names would conflict with the macOS versions, they are prefixed with âgâ, so you have to run gdate instead of date.
To bring this back to KeyboardMaestro, in your a KM macro, add an âExecute shell scriptâ action to run /usr/local/bin/gdate with whatever --date="" string you require (Read over the GNU coreutils date command link above for all the details). Have the action save the output to the clipboard or a KM variable to be used later in your macro.
Note: Be sure to include the full path to the Homebrew bin directory (which is by default /usr/local/bin/) and not just gdate. The shell environment created by KMâs âExecute shell scriptâ action is not the same as when you open a shell in Terminal.app so it wonât be aware of a custom PATH.