How do I make a "Days until (date)" thing?

Does anyone know how to make something that does math based off the date time? For example: I want it to tell me how many days until March 20th 2020 whenever prompted to do so. Any ideas?

1 Like

I'm sure there are lots of way to do this, but here's how I'd do it…

zsh has built-in functionality for strftime which can definitely do this.

First step would be to find the Unix Epoch Seconds for the time that you want to measure to

Let's say that you want to measure to 12:00 noon on 2020-03-20, you'd use this:

#!/bin/zsh -f

zmodload zsh/datetime

strftime -r "%Y-%m-%d %H:%M" "2020-03-20 12:00"

exit 0

which will output 1584720000 but let's save it to a variable instead… how about $TARGET?

And we need to get the current time, also in Unix Epoch Seconds. We use "$EPOCHSECONDS" for that. Then we just need to subtract one from the other. So it would look like this:

#!/bin/zsh -f

zmodload zsh/datetime

TARGET=$(strftime -r "%Y-%m-%d %H:%M" "2020-03-20 12:00")

DIFF=$(($TARGET - $EPOCHSECONDS))

exit 0

Now the variable $DIFF contains the number of seconds from right now until noon on 2020/03/20. But I assume you would that translated to something a bit more "readable". That's where seconds2readable.sh comes in.

#!/bin/zsh -f

zmodload zsh/datetime

TARGET=$(strftime -r "%Y-%m-%d %H:%M" "2020-03-20 12:00")

DIFF=$(($TARGET - $EPOCHSECONDS))

seconds2readable.sh "$DIFF"

exit 0

As I write this, the script tells me the answer is 175 days 12 hours 29 minutes 45 seconds.

So, how do you use this?

  • Download seconds2readable.sh and put it somewhere like /usr/local/bin/seconds2readable.sh

  • Note: if you don't have a /usr/local/bin/ folder, you can create it with this command:

sudo mkdir -p /usr/local/bin/
sudo chown "$USER" /usr/local/bin/

Use this command to download it:

curl -fL --output /usr/local/bin/seconds2readable.sh \
'https://gist.github.com/tjluoma/bb2b3d1c58e52576d61ce11221b8906a#file-seconds2readable-sh'

Now make it executable:

chmod 755 /usr/local/bin/seconds2readable.sh

Then you can put this into Keyboard Maestro. Note that I have added a $PATH line to the script because otherwise Keyboard Maestro might not check /usr/local/bin:

#!/bin/zsh -f

PATH=/usr/local/sbin:/usr/local/di:/usr/local/scripts:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin

zmodload zsh/datetime

TARGET=$(strftime -r "%Y-%m-%d %H:%M" "2020-03-20 12:00")

DIFF=$(($TARGET - $EPOCHSECONDS))

seconds2readable.sh "$DIFF"

exit 0

Put that script into Keyboard Maestro and tell it to output to a window or "display briefly" and I think you'll be all set. Just have to decide how you want to trigger it.

If you want to change it to work for another date, just change the 2020-03-20 part of the TARGET= line. You can also change the time. Just remember to use 24-hour time, so, for example, if you wanted to measure until 4:35pm, you'd use 16:35.

1 Like

TJ, I think there's a builtin token in KM that supports date math.

Use the functions:

to get the different in seconds, then divide by 24 and 3600.

(TIME(2020,3,20)-TIME(YEAR(),MONTH(),DAY()))/24/3600

image

5 Likes

Works well, tysm. Just what I was looking for, peter.

Well, dang… I didn't know Keyboard Maestro could do that. Learned my new thing for the day.

2 Likes

One of the answers mentioned zsh. I know this is the default in Catalina - which I’m not running yet - but is zsh installed by default in eg Mojave?

And yes I prefer the KM answer.

2 posts were split to a new topic: Impact of macOS Catalina Defaulting to Unix Shell using Zsh

2 posts were merged into an existing topic: Impact of macOS Catalina Defaulting to Unix Shell using Zsh