@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
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.
-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