Date Calculations

Are there any built in functions in KeyboardMaestro which allow date calculations? I used to have a database app, Panorama that had built in values available in a formula to calculate the beginning or end of a week, month, quarter, or year. Just now I am wishing I had a way to calculate how many days are left until the end of the month. This is for work on my budget and when I make a text entry I want It to expand to the number of days left in the month. I wrote to the TextExpander folks to see if TextExpander can do this, not sure if It can though.

Example: Today is 22 Aug 2015. If I entered the â€ś;dateEOMâ€ť text, the macro would trigger and convert that to â€ś9â€ť as there are 9 days left in this month. Doing the same thing next month would use a different value for EOM which is what I am asking, if KM has a built in function that can be used in a calculation that always calculates from the EOM (end of month)? Thanks.

UPDATE: TextExpander just said that in Version 5 you can now use JavaScript in TextExpander so if you know that (I donâ€™t) then you can do much more. They also said that right now It is possible to do some calculations although apparently not without knowing a thing or two.

Yes, via the Tokens:

token/ICUDateTime
Date/Time Format Syntax

Yes, I am aware of those. In your example I could say, %ICUDateTimeMinus%3*7%Days%EEE, MMM, d, yyyy h:mm%. That is fine if you know how many days there are until the end of the month. I am seeking a solution that when expanded would always give you how many days are left until the EOM, EOQ, etc.

To do this I would need to have a built in function that already has calculated the end of each month, quarter and year. Months vary with 28, 30 and 31 days so It would have to respond depending on the correct â€śnowâ€ť to make the correct calculation when the string is triggered.

Here are some examples of what I am trying to use this for;

We have xx days left until the end of the month
I sent that information on the first which was xx days ago
We have xx days left in this quarter to come up with a final version of our proposal.
Itâ€™s been xx days since this quarter started and we are not that far along yet.
Itâ€™s been xx days since this year started and we still have to get our goals started.
We only have xx days left until the end of the year to get our tax credits in place so we have to get this project accomplished (lets hurry please).
We only have xx days left until the end of the month so â€¦

Obviously I can use a spreadsheet, database or a project managing app and this function would already be built in. I am asking this here, now, because I would find It useful as plain text when writing emails, documents, etc.

Thanks.

OK, I get it now.

You might do a javascript search for these functions.
Years ago when I played web master, I remember lots of JS date functions being posted on the 'net. In fact, thatâ€™s how I would start â€“ with a search for â€śjavascript date functionsâ€ť

If you find what you need, then you could just plug it in (or adapt it) to a KM macro Execute Javascript action.

Thanks. I really got spoiled when I was using Panorama. It had so many of these sorts of calculations built in already. I have since decided that making that work environment my sole area of work is really too complex for me to keep up with - and still get work done. Using macros that I can trigger globally makes much better sense.

The new version of TextExpander uses JavaScript so I could go that way too as well as using KeyboardMaestro as you suggested. It appears that this is not something that is built into KeyboardMaestro yet. One can hope that this catches Peter Lewisâ€™ fancy but if It does not, I can do more research on javascript as you say.

I tried researching AppleScript solutions and that may be possible in AS using KM as a trigger. But AS is way to confusing for me to make much use of. I was not able to find the solution I was looking for after an hour of using Google and reviewing search results.

AppleScript is far more limited than JavaScript in terms of date functions.
I would think what you are asking for is not uncommon. So Iâ€™d expect to find a JS solution.

http://www.w3schools.com/jsref/jsref_getutcdate.asp

This example doesnâ€™t show me enough to solve the problem, Itâ€™s a start. I am going to have to roll up my sleeves and see if I can actually lean JS as It does seem useful.

Javascript, PHP, Perl and Ruby have all seemed like worthwhile languages to learn. If JS is fully capable, modern and well supported - and currently useful (TextExpander, KeyboardMaestro) then I will see how far I can go with that.

google search on â€śjavascript date functionsâ€ť

Thanks. I am watching several videos right now:

http://stackoverflow.com/questions/11246/best-resources-to-learn-javascript
The JavaScript Programming Language

Recommended for those who want to learn the language from a classroom lecture perspective.

This might be one approach,
though I would not be entirely surprised to learn of a more direct route : - )

(Just under 9 days left here â€“ KM date functions and this custom action deal in numbers of seconds since the start of the 1970s)

1 Like

Do you have a direct link to the videos?
Everything I tried in at the stackoverflow.com post didnâ€™t work.

Thanks for this. I just got back to my seat and will give this a look soon.

OK, no luck there. It is apparently a custom statement that downloads but needs in some way to still be compiled. And I donâ€™t know how to do that without instructions.

And hereâ€™s one link for online streaming:
YUI Theater: Douglas Crockford, The JavaScript Programming Language

Thanks

The JavaScript Programming Language links are redundant, but Iâ€™ll leave them in for convenience.

I generally use Leech.app for such things meâ€™self. Itâ€™s a lightweight and quite scriptable downloader.

I generally use the `youtube-dl` command-line-tool + an AppleScript to pick the version I want + the Terminal for progress-info and feedback.

-Chris

Thanks. I am just getting my feet wet. Apparently Javascript was developed when web browsers were hot and hey, they still are. So I see examples wrapped in html tags which I donâ€™t actually need - in TextExpander. For example if I copy a line that starts with â€śdocument.writeâ€ť TextExpander goes â€śWTF?â€ť No documents here. So I am still at It.

Doing a tutorial just now using Safari/Firefox and a text editor ((TextWrangler).

FINISHED!

OK, for anyone who is interested, here is what I learned (mostly from others work but also some trial and error and new understandings). The following now triggers a macro when triggered in TextExpander:

``````now = new Date()
var d1 = new Date(); //"now"
var d2 = new Date (new Date(now.getFullYear(), now.getMonth() +1 , 1) - 1)
var diff = Math.abs(d2-d1);  // difference in milliseconds
var one_day=1000*60*60*24; // milliseconds
var res1 =  Math.round(diff/one_day);
res1 + " days"``````

Keyboard Maestro has many functions and tokens for calculations like this. Number of days until next month is:

JD(YEAR(),MONTH()+1,1) - JD(YEAR(),MONTH(),DAY())

Note that this will work fine even if MONTH() is December.

4 Likes

Very nice, thank you. Does this also work for quarter?

Also, is there a way to put this in my own time zone? I was just fiddling with this idea in Javascript - still not there yet. Yesterday before midnight Mountain Time I got 9 days left. Today after midnight I am still seeing 9 days left. So, not sure this is right yet?

Date.prototype.diffDays = function (date: Date): number {

``````var utcThis = Date.UTC(this.getFullYear(), this.getMonth(), this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds(), this.getMilliseconds());
var utcOther = Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds());
``````
``````return (utcThis - utcOther) / 86400000;
``````

};

Thanks