I would like a quick way to calculate a certain number of days before a date of my choosing. For example, I would like to be able calculate:
50 days before 2020-08-23
The documentation is pretty clear regarding how to do so with today's date (%ICUDateTimeMinus%50%Days%EEE, MMM d, yyyy%), but I can't figure out how to adapt that expression to a different date that I input myself.
Any assistance would be helpful. I have no development background and am relatively new to using Keyboard Maestro beyond simple text expansion.
%ICUDateTimeMinus% can only be used for the current date, not an arbitrary one. To use an arbitrary date, you need to use the %ICUDateTimeFor% token and the TIME() function with the dates inputted, along with a little math to derive the number of seconds in the number of days specified (the reason being that date and time functions work by calculating the number of seconds that have passed since January 1st 1970, a.k.a. Unix time):
To use an arbitrary date, you need to use the %ICUDateTimeFor% token and the TIME() function with the dates inputted, along with a little math to derive the number of seconds in the number of days specified (the reason being that date and time functions work by calculating the number of seconds that have passed since January 1st 1970, a.k.a. Unix time):
This makes sense. I'll download your .kmmacros file and give it a try!
It can be very challenging to use native KM Actions to do date calculations.
I spent a lot of time a couple of years ago trying to make sense of all of this, and came to the conclusion that for date calculations, it is easier to use a simple shell script.
When using TIME like that, and when only interested in days, it is highly recommended that you make it the middle of the day (so add “,12,0,0” to the TIME parameter). Otherwise you are likely to be off-by-one on your return in half the world, since TIME works in GMT, and date displays are in local times, which for midnight is the day before for half the world (particularly the Ammericas).
BTW, you can simply do TIME(Year,Month,Day-Offset,12,0,0), for example: