Date Math with Keyboard Maestro

Hi,

I am trying to find a way to construct a macro which allows inputting a date, then inputting a number of days to be added to the previously inputted date, and outputs the resulting date. So, I input something like 6-2-15. I want the macro to tell me what date it will be if I add 60 days to that date, so I input the number 60 (or the macro could have the number 60 baked in), and I get as the result August 1, 2015 (or 8-1-15 or what have you).

Any help would be appreciated. Thus far, I can only find macros which use the current date, I can't figure out how to input an arbitrary date.

Thanks,
Brian

It might be worth running man date in Terminal.app and looking at the -v adjustment option for the Bash date command.

At the Terminal prompt, where I am sitting,

date -j -v+60d -f %m-%d-%y "6-2-15"

generates

Sat  1 Aug 2015 02:00:56 BST

and you could assemble that kind of string in a Shell Execute action using $KMVAR_variable_name references to your KM variables.

Hence something like:

date delta.kmmacros (2.3 KB)

date -j -v$KMVAR_adjustment -f %m-%d-%y "$KMVAR_base_date"
1 Like

Here is one way of doing it.

See also Regular Expression Typed String Example for how you could trigger this with a typed string trigger that included the date fields. And if you want to use the date format like "6-2-15", use the Search Variable action to break the variable into its component parts, like this:

Here are two links that may be useful...

http://knowit.co.nz/2011/03/report-the-time-in-another-city-with-applescript

http://erikslab.com/2007/11/26/date-time-calculations-using-applescript/

Thank you. I’m not adept at using the terminal but I will fool around with it.

Peter, I tried the first method, which is very promising. However, when I ran the macro, the calculation seemed to assume that each month has 30 days, so the calculation would be off by a day. Am I missing something?

Hey Brian,

I would probably use @ComplexPoint's macro. He's already done all the work for you, and the Unix date command is very well tested.

On the other hand this sort of thing can be done pretty easily with AppleScript, and I would probably trust it for this kind of simple date-math.

------------------------------------------------------------
set dateVar to current date
set time of dateVar to 1
set newDate to text returned of (display dialog "Enter a date of format 'MM-DD-YYYY'" default answer "MM-DD-YYYY")
set AppleScript's text item delimiters to "-"
set {_month, _day, _year} to text items of newDate
tell dateVar
  set its month to _month
  set its day to _day
  set its year to _year
end tell
set dateVar to dateVar + (60 * days)
set AppleScript's text item delimiters to "T"
set dateVar to first text item of (dateVar as «class isot» as string)
------------------------------------------------------------

In this case 60 days is baked-in, but that's really easy to change.

--
Best Regards,
Chris

The calculations do not assume months are all 30 days.

However you can be off by one day if you are not careful with the time of the date. The TIME function works in GMT, so if you do not include the “12,0,0” (ie, 12 noon) on the day, then for those of you with negative time zones can see the day before. By placing the date at 12 noon GMT, pretty much everyone has the same date.

Peter, you’re absolutely right. I had stripped out the 12,0,0 because for some reason I didn’t think it was necessary. Should’ve had faith! This macro works perfectly now and I can’t thank you enough.

Thanks to all of you who responded with creative solutions. I learned something new from all of the ideas suggested.