Macro for date of last Monday, Tuesday, etc?

I have a repetitive task where I need to take a day of the week (this Monday, this Tuesday, etc.) and convert it into a date. I've read the forum post on date anchors and I have other macros using simpler ICU offsets, but haven't been able to piece together how to use the offsets to get what i want.

My ideal behavior would be to invoke a macro like kkmonday or similar, and spit out something like 26-Nov-2018. I always do this task on a Friday, so I really only need to know the most recent Monday, Tuesday, etc.

Thanks for any tips!

Not sure if this is the "real" Keyboard Maestro-way, but I would probably just do this using a shell script and your favorite programming language.

I used PHP here, but any language would do.

Essentially the whole macro is one "Execute Shell Script" with no input, and "paste results" output.

The script:

#!/usr/bin/env php
<?php
$d = new DateTime("Last Monday");
print $d->format("d-M-Y");

You can use whatever trigger you like :slight_smile:

Next Monday.kmmacros (1.7 KB)

For more info about the date formats (I used d-M-Y, from your example) you can check out http://php.net/manual/en/function.date.php. Just use whatever letters give you the right result. :wink:

I believe this macro should do the job:
MACRO: @DATE Get Date for Prior/Next Day of Week [Example]

You will probably need to modify the last action or two to output in the exact format you want.

Check it out and let us know if it works for you, or you have question.

Using Keyboard Maestro 8.x, you can use the extended options in the ICUDateTimeMinus token, like this:

%ICUDateTimeMinus%1%Tuesday%d-MMM-yyyy%
5 Likes

That is very cool, Peter. I had forgotten about this new feature in KM 8+.

1 Like

That's amazing @peternlewis, exactly what I was looking for! Thank you!

1 Like