This causes some compromises in time, and complications in Actions to use.
The below macro presents one simple Shell Script to made date calculations easy, while providing much flexibility
Since ALL of the KM Variables are outside of the actual script, and are entered as Parameters to the Action, it makes changing them easy and reduces errors.
# Enter KM Variables (or text) for these Bash Variables
# BaseDate AdjustBy [ AdjustUnits DateFormatIN DateFormatOUT ]
# Variables in brakets [ ] are optional; Must enter prior Variables to not enter subsequent variables.
# Default for Both input and output date format is YYYY-MM-DD
read myBaseDate myAdjust myAdjustUnits myDateFormatIn myDateFormatOut
# --- DEFAULT VALUES If NOT Passed Via stdin ---
myAdjustUnits=${myAdjustUnits:-d}
myDateFormatIn=${myDateFormatIn:-%Y-%m-%d}
myDateFormatOut=+${myDateFormatOut:-%Y-%m-%d}
# Adjustment Input Format Output Format
date -j -v$myAdjust$myAdjustUnits -f $myDateFormatIn "$myBaseDate" $myDateFormatOut
Provide a Reusable, Configurable, Action to Calculate Dates
While KM provides extensive handling of dates using the ICU tokens & functions, it is very laborous to write a macro which calculates a new date from a date other than the current date.
The Action in this macro presents an alternative to using KM ICU tokens.
It provides one KM Action, a Shell Script with a Bash date command, to accomplishes this with only 5 lines, and yet is very configurable.
It only requires two inputs, which the macro author can enter as KM Variable Tokens in the Execute Shell Script Action Input area, which are Parameters for the script Standard In (stdin):
Base Date
Adjust by amount (in days, +/-)
By Default, the script uses this data, which can be easily overridden:
Adjust By Units: Days
Date Input Format: ISO 8601 YYYY-MM-DD
Date Output Format: ISO 8601 YYYY-MM-DD
Override these by adding Parameters to the Script Input area.
If you don't like the defaults, you can easily change the defaults in the script.
REQUIRES:
KM 7+
macOS 10.10+ (Yosemite)
However, it has been tested ONLY in KM 8.2.2 with macOS 10.12.6
How To Use
As a Demo
Just Trigger this macro.
Enter the Base Date and Offset
Observe Results
As a Reusable Action
Save the Execute Shell Script Action somewhere for easy reuse
(choose one of these:)
Thanks! That's super helpful and exactly what I was looking for.
However, I would like to make a small adjustment and haven't figured out how.
I would like to show for the OUTPUT the date like "Fr, 2020-04-17". Can that be easy done? I tried a bit, but it's not working. I am anything but an expert to be fair.
It is easy to do once you know how, but a bit tricky in the setup. In order to include a space in the format, the format string must be in quotes.
Use this script for the shell script:
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Title: Date Calculation
# Ver: 1.1
# Date: 2020-04-17
# Author: JMichaelTX
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Enter KM Variables (or text) for these Bash Variables
# BaseDate AdjustBy [ AdjustUnits DateFormatIN DateFormatOUT ]
# Variables in brackets [ ] are optional; Must enter prior Variables to not enter subsequent variables.
# Default for Both input and output date format is YYYY-MM-DD
read myBaseDate myAdjust myAdjustUnits myDateFormatIn myDateFormatOut
# --- DEFAULT VALUES If NOT Passed Via stdin ---
myAdjustUnits=${myAdjustUnits:-d}
myDateFormatIn=${myDateFormatIn:-%Y-%m-%d}
# myDateFormatOut=+${myDateFormatOut:-%Y-%m-%d}
myDateFormatOut=+${myDateFormatOut:-"%a, %Y-%m-%d"}
# Adjustment Input Format Output Format
date -j -v$myAdjust$myAdjustUnits -f $myDateFormatIn "$myBaseDate" "$myDateFormatOut"
This macro is fantastic - it goes a long way to meeting a need I've had. But I can't figure this out --
What's the best way to get new dates around the target date?
For example, I'm trying to output a text string that looks something like this:
| [[week # of target date]] | [[day before target date]] | [[target date]] | [[day after target date]] |
This serves as a breadcrumb line at the bottom of text files, with wiki-links to the next and previous days
The formatting isn't an issue, and I can parse out the day, month, year, etc. I just can't figure out how to create a new date based on the %Local__AdjustBy% variable, or the %Local__NewDate% result.
I tried to adjust the value of %Local__AdjustBy% later in the macro by adding one and subtracting one, but it won't work. The macro always fails; I set the calculation of a new variable to Local__AdjustBy + 1, for example, and nothing happens.
While KM provides extensive handling of dates using the ICU tokens & functions, it is very laborous to write a macro which calculates a new date from a date other than the current date.
The Action in this macro presents an alternative to using KM ICU tokens.
It provides one KM Action, a Shell Script with a Bash date command, to accomplishes this with only 5 lines, and yet is very configurable.
It only requires two inputs, which the macro author can enter as KM Variable Tokens in the Execute Shell Script Action Input area, which are Parameters for the script Standard In (stdin):
Base Date
Adjust by amount (in days, +/-)
By Default, the script uses this data, which can be easily overridden:
Adjust By Units: Days
Date Input Format: ISO 8601 YYYY-MM-DD
Date Output Format: ISO 8601 YYYY-MM-DD
Override these by adding Parameters to the Script Input area.
If you don't like the defaults, you can easily change the defaults in the script.
REQUIRES:
KM 7+
macOS 10.10+ (Yosemite)
However, it has been tested ONLY in KM 8.2.2 with macOS 10.12.6
How To Use
As a Demo
Just Trigger this macro.
Enter the Base Date and Offset
Observe Results
As a Reusable Action
Save the Execute Shell Script Action somewhere for easy reuse
(choose one of these:)
This is great, thank you so much @JMichaelTX . I get the wrong week number for some reason, and I've seen the fix on the forum before, though I can't recall what's causing the problem.
The output says 09-14-2020 is in week 37, but Fantastical says it's week 38. Not sure which is correct. Do I need to account for time zone (US CDT), or because I have my weeks starting on Sunday instead of Monday, like some calendars? This site seems to confirm my calendar. Also did some digging here but not sure the "YYYY" vs "yyyy" vs "y" issue is relevant.
OK, that part's working again, but I'm still getting week #43 for October 31, 2020, when my calendar says week #44. I like a puzzle, but this is driving me crazy. I'll keep digging.
Yes, that does work. Bugs me as to why, since my calendar is set specifically to have Sunday as the first day, whether I choose Gregorian or ISO calendars. Same functionality with U and V in the macro, either way. Crazy. But it works. Sometimes the answer that looks wrong is actually the one that is most expedient. Therefore the hardest to find. Thanks again for your help!
@JMichaelTX thanks for the great help. Is there any way to adjust the language of the date output?
I have been looking around, but cannot find any solution.
It is bad since Mac Big Sur. I have seen in the "Region and Language" Setting one can add apps and adjust the language. However, Keyboard Maestro does not work since "Keyboard Maestro does not support other languages."
Is there are another way but to use search and replace?