Date Calculations

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. :smile:

Thanks. I am starting to research how to learn more about using Javascript.

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.

There's lots of JS date functions already built that can help you get started:
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
Douglas Crockford: “The JavaScript Programming Language”/1 of 4 [M4V download]
Douglas Crockford: “The JavaScript Programming Language”/2 of 4 [M4V download]
Douglas Crockford: “The JavaScript Programming Language”/3 of 4 [M4V download]
Douglas Crockford: “The JavaScript Programming Language”/4 of 4 [M4V download]

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.

No, that’s why I posted those download mp4 titles. Go to that page and find those links. They all work to download the videos. The direct page links do not seem to 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.

Here are the download links:
Douglas Crockford: "The JavaScript Programming Language"/1 of 4 [M4V download]
Douglas Crockford: "The JavaScript Programming Language"/2 of 4
Douglas Crockford: "The JavaScript Programming Language"/3 of 4
Douglas Crockford: "The JavaScript Programming Language"/4 of 4

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.

If you don’t know — you can simply paste these links into Safari’s download sheet, and they will be queued for download.

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

http://yui.zenfs.com/theater/crockford-advancedjavascript-1.m4v
http://yui.zenfs.com/theater/crockford-advancedjavascript-2.m4v
http://yui.zenfs.com/theater/crockford-advancedjavascript-3.m4v
http://yui.zenfs.com/theater/crockford-domtheory-1.m4v
http://yui.zenfs.com/theater/crockford-domtheory-2.m4v
http://yui.zenfs.com/theater/crockford-domtheory-3.m4v
http://yui.zenfs.com/theater/crockford-tjpl-1.m4v
http://yui.zenfs.com/theater/crockford-tjpl-2.m4v
http://yui.zenfs.com/theater/crockford-tjpl-3.m4v
http://yui.zenfs.com/theater/crockford-tjpl-4.m4v

The YouTube link JM posted I believe is the full-length version of “The JavaScript Programming Language”, and that is easy enough to download with Firefox and the FlashGot extension or another YouTube 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

@peternlewis Can you please say more about using specific dates? EOM is good but I need time to and from specific dates.

Keyboard Maestro “Calculate Specific Date in Seconds” Macro

Calculate Specific Date in Seconds.kmmacros (4.1 KB)

This produces Epoch Seconds.

You can do something similar with the Julian Date function like so:

%Calculate%JD(2015, 9, 27, 20, 34, 30)%

-Chris

1 Like

Thanks, I appreciate that. I finally got the following to work using TextExpander as their v5 now allows Javascript:


Javascript:
var one_day=10006060*24; // milliseconds
var d0 = new Date()
var d2 = new Date("October 1, 2015 12:00:00"); // "SOME EVENT"
var word2 = d2 > d0 ? "until" : "since";
var diff2 = Math.abs(d0-d2); // difference in milliseconds
var res2 = Math.round(diff2/one_day);
"- " + res2 + " Days " + word2 + " 10/01/2015 - SOME EVENT“ +"\n"+

Result:

  • 03 Days until 10/01/2015 - SOME EVENT

Using Peters suggestion of:
JD(YEAR(),MONTH()+1,1) - JD(YEAR(),MONTH(),DAY())

I was wondering how I could interpret this sort of string to do a similar thing in KM as I did using JS in TE? My example uses or has code in place for 6 different events for tracking purposes so once I get It figured out I plan to reuse the code.

I don't monkey with date math much, so I don't have a canned solution – but I've shown you how to calculate epoch seconds – from there you can generate ES for two date/times subtract them and turn it into days.

There may be simpler ways to do this with KM, but you'll have to wait for Peter.   :wink:

-Chris