Calculate days between two dates

I have poured over the forums here and I have found some ideas to what I need but nothing that really fits the bill.

What I have are dates in either MM/dd/yy or MM/dd/yyyy (ex. 10/05/18 or 05/24/2019)

Date1 would be formatted like 10/05/18

Date2 would be formatted like 05/24/2019

What I would like to do is take Date1 10/05/18 and calculate 180 days after that date which would be Date3 04/03/19.

I would then like to calculate if Date2 05/24/2019 falls after Date3 04/03/19

I would like for the calculation to also work regardless of year. So if 180 day crosses the calendar year from December to January the calculation should still work.

What I have found doesn't overcome this issue, which means I just don't understand KM well enough.

I've been playing with ICU formats but I am no programmer. I'd like to do this all in KM if possible but certainly open to AppleScript or JavaScript. Thanks!

For this use case I find AppleScript to be the best tool for the job. It has a date detector that will identify a date from a wide variety of formats.

Let us know if this works for you.

You will need to enter the date data in the first Action using this format:

<date 1>
<date 2>
<days to add>

For example:

10/05/18
05/24/2019
180

Example Output

image

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

MACRO:   Extract Dates, Do Date Math, & Compare [Example]

**Requires: KM 8.2.4+&nbsp;&nbsp;&nbsp;macOS 10.11 (El Capitan)+**
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

#### DOWNLOAD Macro File:
<a class="attachment" href="/uploads/default/original/3X/b/b/bb326d67940d9927c586267785fe950b2ef8a3e3.kmmacros">Extract Dates- Do Date Math- & Compare [Example].kmmacros</a>
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**


---

### ReleaseNotes

Author.@JMichaelTX  based on script by @ccstone

**PURPOSE:**

* **Extract Dates, Do Date Math, & Compare **
   * Uses AppleScript Date Detector

**HOW TO USE**

1. First, make sure you have followed instructions in the _Macro Setup_ below.
2. Trigger this macro.

**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.  ??
.
**Make These Changes to this Macro**
1. Assign a Trigger to this macro.
2. Move this macro to a Macro Group that is only Active when you need this Macro.
3. ENABLE this Macro, and the Macro Group it is in.
.
* **REVIEW/CHANGE THE FOLLOWING MACRO ACTIONS:**
(all shown in the magenta color)
   * Enter Your Date Data Here 
     * Must be in the format:
```
<date 1>
<date 2>
<days to add>
```


**REQUIRES:**

1. **KM 9.0+** (may work in KM 8.2+ in some cases)
2. **macOS 10.11.6 (El Capitan)+**

TAGS:  @Dates @AppleScript @ASObjC @DateMath
<img src="/uploads/default/original/3X/3/6/361ce06b4f735faf0d23c400f8d69242d46ce8ff.png" width="661" height="1203">
1 Like

Convert the days to Year, Month, Day variables.

Normalise the year to 20xx.

Use the JD function to calculate the difference in days.

Keyboard Maestro Actions.kmactions (3.8 KB)

2 Likes

Amazing! This did the trick! I have a better understanding of the time calc functions now. Thanks again!!!