If...then...condition to run only during date range

Is there a way to create a trigger based on today being between a date range?

e.g. if I want something to run only if today is between 7/1 and 7/30.

I’ve tried setting a variable TODAYSDATE to %ICUDateTime%MMdd%
Then I created an If/Then to try to set a Calculation as being 0701<TODAYSDATE<0730

But that’s coming up as false, even though I see TODAYSDATE is currently 0725

This doesn’t even work if I just sent the Calculation to TODAYSDATE<0730

Is there a way to do this?

Hey @edjusted,

Sure – but date-math is hard to wrap your head around to start with.

“0725” is completely meaningless unless you translate it into a format suitable for calculations.

See this post (and the thread in general):

See also this page on the Wiki:

https://wiki.keyboardmaestro.com/token/ICUDateTime

If you calculate Epoch Seconds for 7/1 and 7/30 then comparison is simple:

%Calculate%TIME(2016, 7, 1)%
--> 1467331200

%Calculate%TIME(2016, 7, 30)%
--> 1469836800

%Calculate%NOW()%
--> 1469483562

Here's a basic example:

Now() is Between Two Dates.kmmacros (2.1 KB)

-Chris

One way to do this is to get the date in YYYYMMDD format and pretend it is an 8 digit number.

For example:

Keyboard Maestro Actions.kmactions (0.9 KB)

And if you'll note the condition is currently saying "currently false" and the reason for that may be the same issue you have - the macro has not been run! So the actual current value of Today is (as shown in the action) "2016-07-26" which is not a legitimate number, hence the calculation is invalid. But if I select Try on the first action, then the second one now has a valid, true calculation and changes to "currently true".

4 Likes

Thanks all! I was trying to make this a macro that could run yearly, thus the “0725” instead of using the entire date.

It's kind of a silly experiment, but the whole story is, we have an old Mac Mini that does nothing but play music for our telephone hold system. I wanted to set it so it plays certain playlists during certain times of the year (e.g. Holiday music during the holidays).

The whole idea was to automate everything so no one would ever have to touch it. So I turned the Year into a variable.

Then I tried a few combinations of what @ccstone and @peternlewis suggested but still can't get it to work.

Here, I tried to incorporate a dateTest but it seems to act as an "OR" test than an "AND" test. That is, if Today is greater than the start date OR less than the end date, it's true. But I want it as an "AND".

Then I tried it as a direct calculation, but it keeps coming up false no matter what I set the start and end dates to.

Maybe I'm missing something?

Oops, on “Then I tried it as a direct calculation, but it keeps coming up false no matter what I set the start and end dates to.” I should say that it keeps coming up True…

What I demonstrated would work equally well without the year. Just remove the yyyy from the first action, and the 2016 (twice) from the second action.

Your action appears correct to me, and yet is showing “currently false”. My guess is that you have other characters in the calculation field that are not visible in the image. Try widening the display, or alternatively, post the actual actions (see How export a macro as an image without starting a new post?).

“A < B < C” (like you show in your second example) is not what you expect. It does not mean “A < B AND B < C”. “A < B” returns 0 or 1, and then it will test 0 or 1 < C, which in this case will always be true for “< 20161201”.

Ok, I got it. Thanks Peter! (I think I was having trouble because I'm using an older version of Keyboard Maestro on that Mac...version 5. Maybe the "AND" in calculations doesn't work back then? Sorry, I should've mentioned version number earlier.

Just for reference, here's an animated GIF showing why/how it's not working:

Here's my workaround, which is working fine:

AND works in the older version, the problem is that the older version is not smart enough to figure out that "Today AND Today" is not a variable (since it is a legitimate variable name).

The newer version is.

In the older version, use brackets to ensure correct parsing.

(0701 <= Today) AND (Today <= 0730)