So the subroutine should handle both of those situations. I can certainly anticipate some situations in which you want next to mean strictly next, in which case the correct answer is 7, and others in which you want it to mean today or next, in which case 0 is correct. Suppose we want the number of days until the next Thursday. \Īnd the number of days until the previous Saturday isĪll of which are the answers we expect, including the signs. Similarly, to get the number of days until the previous Monday, it’s The number of days until next Saturday (7) is If it’s Thursday (5) and I want to know how many days until the next Monday (2), the calculation is And adding or subtracting multiples of 7 doesn’t change the remainder. By doing so, I force the value before MOD to be positive or negative as needed. This is why I add 7 in the “next weekday” formula and subtract 7 in the “previous weekday” formula. The important thing to know here is that Keyboard Maestro’s MOD operator returns a positive number if the number before MOD is positive and a negative number if the number before MOD is negative. Then if we’re looking for the next target weekday, we do this calculation, (LocalTargetWeekday - DOW() + 7) MOD 7Īnd if we’re looking for the previous target weekday, we do this (LocalTargetWeekday - DOW() - 7) MOD 7 Let’s say the number of the target weekday is stored in the variable LocalTargetWeekday. The return value ranges from 1 (Sunday) to 7 (Saturday) instead of 0 to 6, but we can work around that. Keyboard Maestro has a DOW() function which, if given no argument, returns an integer representation of today’s day of the week. Ideally, the language you’re programming in assigns the numbers 0 through 6 to the days of the week (which weekday is Day 0 doesn’t matter as long as you know what it is) and you add or subtract days mod 7 to get the weekday of interest. So by having my subroutine return the number of days until or since the weekday of interest, I can plug that return value into the %ICUDateTimePlus% token in the macro that calls the subroutine.ĭate calculations of this type (when is the next/previous weekday?) are typically done using modulo arithmetic, which is basically division where you return only the remainder. So, for example, if it’s Thursday (which it is as I type this) and I want the date of last Sunday in the format “,” I could call %ICUDateTimePlus%-4%Days%d MMM yyyy% Keyboard Maestro has a token, %ICUDateTimePlus% that allows you to specify a date in the past or future and format it however you like. While I could have the subroutine return a date string, like “,” that would make it difficult to use in macros that want a date string in a different format. Most import, Keyboard Maestro doesn’t have a Date type.There are two reasons I decided to have the subroutine return a number instead of a date: The calculations could be done in the same subroutine, with the difference being that upcoming days return a positive number and previous days return a negative number. And while I’m at it, I should do the same for the most recent Monday, Tuesday, etc. The release of Keyboard Maestro 10, with its addition of subroutines, and a couple of recent questions on the Keyboard Maestro forum regarding date calculations got me thinking that I should make a subroutine that calculates how many days it will be until the next Monday, Tuesday, Wednesday, or whatever weekday. Next post Previous post A date calculation Keyboard Maestro subroutine
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |