Date Calculator [Reusable Action]

Use Case

  • Calculating a Date in the future or past that is NOT based on Current Date (although this macro/action will work for current date as well).
  • Basing it on any other date requires use of the ICUDateTimeFOR token with the TIME function which is in GMT.
    • 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.
  • It can easily be refactored as a reusable Action.

Reusable Execute Shell Script

# 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

Example Output

image

image


MACRO:   Date Calculator [Reusable Action]

~~~ VER: 2.0    2018-07-23 ~~~

DOWNLOAD:

Date Calculator [Reusable Action].kmmacros (12 KB)
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.

As always, please feel free to post any questions, issues, and/or suggestions you may have about this macro.


ReleaseNotes

Author.@JMichaelTX

PURPOSE:

  • 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:

  1. KM 7+
  2. macOS 10.10+ (Yosemite)
    • However, it has been tested ONLY in KM 8.2.2 with macOS 10.12.6 :wink:

How To Use

As a Demo

  1. 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:)
    1. I recommend the KMFAM Macro System by @DanThomas
    2. Setup as a Sub-Macro, which can be called using an Execute Macro Action
    • Paste the Script Parameters as Parameters to the Execute Macro Action
    1. Store in a "template" macro where you can copy/paste into new Macros

MACRO SETUP

  • Carefully review the Release Notes and the Macro Actions
    • Make sure you understand what the Macro will do.
    • You are responsible for running the Macro, not me. ??
      .
  1. Assign a Trigger to this maro..
  2. Move this macro to a Macro Group that is only Active when you need this Macro.
  3. ENABLE this Macro.
    .
  • REVIEW/CHANGE THE FOLLOWING MACRO ACTIONS:
    (all shown in the magenta color)
    • Prompt for User Input
      • You can change the defaults for BaseDate and AdjustBy (which MUST include the sign), but do NOT change the Units.

3 Likes

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.

Thanks!

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"

The key change is this line:

myDateFormatOut=+${myDateFormatOut:-"%a, %Y-%m-%d"}

For the complete list of formatting codes, see
How To Format Date For Display or Use In a Shell Script

Thanks, that's great news. Thanks a lot!