Do date calculations from a specific date rather than current date

I was trying to make this macro: 1) type a date (formart: m/d) in a prompt user input 2) find out the date which is one day ahead of the date I just typed in the prompt 3) insert the calculated date to text by pasting.

I now from reading the articles and wiki how to do that based on the current date, yet I'm lost totally in doing that based on a specific date I can type in. I tried to search the forum but didn't find a solution. Can anybody help? Many thanks!

It's much more complicated to calculate the difference between dates than you think.

Search Date Calculate or calculate between dates you will get some topics:

1 Like

Thank you suliveevil. I've read the articles you kindly attached in your reply. Sadly, I still don't find the exact thing I'm look for: calculate date based on any specific date from my input.

See this Alfred Date Calculator workflow then you will know it's very complicated.

There are leap years、different days in each month and other problems to solve, you need to use a programming language (AppleScript、JavaScript、Lua、Python、Ruby……)to create your own set of scripts to achieve what you wanna get.

If you don't wanna start from scratch,you can search for an online date calculator to do the same thing.

Alright. Let me simply switch to another solution . Thank you anyway.

It doesn't have to be. Every date calculator need not be a general purpose calculator that handles every edge case. :wink:

Don't give up so easily. :wink:
Let's take another look at your requirements and see if we can find a solution for your specific use case.

That should be easy enough.

Actually, I already had a macro that does most of what you want.
I've adjusted it to use your date format. IAC, it should be enough for you to use and adapt to your specific needs.

Let us know if this works for you, or you have further questions about your OP.


==UPDATED==: 2019-03-01 20:03 GMT-6
Just updated the macro to:

  1. Change output date format to match input format: M/D
  2. Actually paste the calculated date.

Example Output

image

image


MACRO:   Date Calculator Custom [Reusable Action]


#### DOWNLOAD:
<a class="attachment" href="/uploads/default/original/3X/2/e/2e1d8dc53b07e86fba1ff33486c0931b6802b52f.kmmacros">Date Calculator Custom [Reusable Action].kmmacros</a> (13 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

---

### 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:  M/D/Y
     * Date Output Format:  M/D/Y
     * 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
     3. 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.

---

![image|473x1509](upload://fHNSUqc6bs5E0btvLd5gbu9voYn.png)

Great job!

1 Like

Thank you for both your encouragement and macro, Michael! I've already embedded it into my workflow and your macro modified for me is also a great example to build something upon it by my own.