Date Calculation

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.

Thanks!

%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):

Calculate Days Before Specified Date.kmmacros (2.6 KB)



Thank for the quick response.

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!

This is a simple enough macro that I'm going to try building into an email response template. Thanks again.

1 Like

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.

MACRO: Date Calculator [Reusable Action]

You may also be interested in this KM Wiki article: Mixing Functions and Tokens

Good luck, and let us know how it goes..

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:

%ICUDateTimeFor%TIME(2020,5,28-50,12,0,0)%yyyy-MM-dd%

Returns “2020-04-08”.

1 Like