Date Calculations

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

AppleScript actually handles date calculations very well.

Here is AppleScript code that handles every example you gave:

on firstOfNextMonth from theDate -- returns a date
	copy theDate to d
	set d's day to 32
	set d's day to 1
	d -- return the resulting date
end firstOfNextMonth

on DaysLeftInMonth for theDate
	return (daysInMonth for theDate) - (theDate's day)
end DaysLeftInMonth

on daysInMonth for theDate
	return 32 - ((theDate + (32 - (theDate's day)) * days)'s day)
end daysInMonth

on getQuarter for theDate
	return ((theDate's month) + 2) div 3
end getQuarter

on quarterStartDate for theDate
	copy theDate to d
	set d's month to ((getQuarter for theDate) - 1) * 3 + 1
	set d's day to 1
	return d
end quarterStartDate

on quarterEndDate for theDate
	copy theDate to d
	set d's month to ((getQuarter for theDate) - 1) * 3 + 3
	set d's day to 32
	set d's day to 1
	set d to d - 1 * days
	return d
end quarterEndDate

on dayOfTheYear for theDate
	copy theDate to d
	set d's day to 1
	set d's month to 1
	return ((theDate - d) / days) + 1 as integer
end dayOfTheYear

on daysLeftInYear for theDate
	copy theDate to d
	set d's month to 12
	set d's day to 32
	set d's day to 1
	set d to d - 1 * days
	return (d - theDate) / days as integer
end daysLeftInYear

on DaysLeftInQuarter for theDate
	return ((quarterEndDate for theDate) - theDate) / days as integer
end DaysLeftInQuarter

on DaysSinceQuarterStart for theDate
	return (theDate - (quarterStartDate for theDate)) / days as integer
end DaysSinceQuarterStart


set DayLeft1 to DaysLeftInMonth for current date
set DayLeft2 to DaysLeftInMonth for date ("7/30/2015")
set theQStart to quarterStartDate for current date
set theDaysLeftInQuarter to DaysLeftInQuarter for current date
set theQEnd to quarterEndDate for current date
set theDaysSinceQuarterStart to DaysSinceQuarterStart for current date
set theDayOfYear to dayOfTheYear for date ("3/1/2015")
set theDaysLeftInYear to daysLeftInYear for date ("12/31/2015")

Note that dates returned from AppleScript subroutines are passed to Keyboard Maestro as a text string which is dependent on your date/time formatting set in the System Preferences. In my English system that is returned as "date Monday, October 12, 2015 at 1:44:41 AM".

You can create any type of formatting you would like in AppleScript, however, by manipulating the elements of the date object to fit what you would like.

More details at:

Macscripter Date & Time discussion

AppleScript Date & Time formatting example

Apple's AppleScript Documentation

The same code can be used in TextExpander, by the way, although I do most of my macro work in Keyboard Maestro.

Hope this works for you,

Eric