Ultimately, I'd like to add a random number of minutes to a date variable to trigger a macro. I'm referencing this suggestion since it's similar in nature, but it seems to be picking a random time from NOW not a variable. What I'd like is for the macro to run at a random number of minutes after 5:30P today. And also this to see how string ICUDate time variables are used.
To get my answer, I'm trying to work in stages. I'm stuck with trying to convert a date string into a calculation field so that I can add a number of minutes to it (in this case I'm using a static 15 minutes). I've tried to use ICUDateTime, ICUDateTimePlus, and other things to make this work but it doesn't.
From a programming perspective (since that's what I'm familiar with), the way I understand how it's working is:
if a variable is set using the "Set Variable to Text" then it is a string variable.
If a variable is set using the "Set Variable to Calculation" then it is an integer, or time from start in this case.
I have to have the variable as an integer in order to add minutes to it, similar to how I would add 15 minutes from now: %ICUDateTimePlus%15%Minutes%MM/dd/yyyy%
ICUDateTime will convert from integer variable to string.
But how do you go from string to integer?
Maybe my understanding is not correct, so please correct me if wrong.
Here is where I'm stuck:
ICUDateTimePlus under the heading Add Time to Date on this page:
Dates and Times [Keyboard Maestro Wiki]
and more generally:
token:ICUDateTime [Keyboard Maestro Wiki]
Yes, I saw those pages as well. My understanding of reading them is that they return the current time:
- %ICUDateTime%[output date format]%
- Returns the current date and time in the specified format.
- %ICUDateTimeFor%[unixtime]%[output date format]%
- Returns Date/Time specified by the unixtime, in the user's local time zone.
- %ICUDateTimePlus%[amount of time]%[unit of time]%[output date format]%
- Returns Date/Time calculated by the Current Date/Time plus the [amount of time].
- %ICUDateTimeMinus%[amount of time]%[unit of time]%[output date format]%
- Returns Date/Time calculated by the Current Date/Time minus the [amount of time].
What I'm looking for is similar to ICUDateTimePlus but for a specific date and time that I supply. Am I missing something?
All Keyboard Maestro variables are, of course, named strings, and when we want to perform numeric operations on them we need either:
set Variable to Calculation action (to specify that the string should be interpreted, and operated on, numerically, or
%Calculate%...% token wrapped around the numeric part of some context that is otherwise non-numeric.
For date-time calculations of any kind, we need to get the time stamp(s) into an integer string format (seconds elapsed since the UCT midnight of 1970).
(Numeric operations have no definition or meaning for formatted calendar strings)
So, breaking it into two stages, something like this general pattern:
Added Seconds.kmmacros (1.7 KB)
And for more general calculations like:
- the start of today (strike of midnight last night)
- some other given time today (e.g. 17:30)
Some time today.kmmacros (3.6 KB)
Thanks again, this is helpful. The last thing I'll try to piece together is adding 15 minutes to the time via calculation (I'll need this calculation based on my original task which is to add a random number of minutes to the time). It's not obvious right now, but I'll try and see if I can solve that. I imagine that I'll have to I need to add 900 seconds to the someMoment variable and then convert back to string with ICUDateTimeFor. Again, I appreciate the guidance.
Got it, thanks. Rather than add 15 minutes, I used your pointer for TIME(), to randomize the minutes place like this:
TIME(YEAR(), MONTH(), DAY(), 17, RAND(30)) - GMTOFFSET()