How would I compare the differences between time in 2 variables?
For example:
MyVar1= 2:30 PM
MyVar2 = 5:45 PM
With the difference between MyVar1 and MyVar2
Result:
"Duration: 3 Hour(s) and 15 Minutes" (or "Duration: 3.15 Hour(s)" will also work)
I tried using How to calculate time differences? as a guidance but was unsuccessful. Are there any examples of this or explanation? Most I have found are more elaborate calculations with dates. All mine are same day. Thanks!
The post you found definitely could solve what you need, but likely there are easier ways to do what you need, if you want only difference of time (and no day, month, year).
Something like this should work: Time Difference Calculations.kmmacros (21.1 KB)
While this task can definitely be done in KM non-script Actions, it seems a bit laborious and verbose to do so. So here's a solution using JavaScript for Automation (JXA).
Questions?
Example Output
MACRO: Calculate Difference in Time [Example]
#### DOWNLOAD:
<a class="attachment" href="/uploads/default/original/3X/f/e/fe0563aa31d0f50eda17990df9eee77ae1e3cc9c.kmmacros">Calculate Difference in Time [Example].kmmacros</a> (11 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**
---
### ReleaseNotes
Author.@JMichaelTX
**PURPOSE:**
* **Calculate Difference in Time Strings**
**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. ??
.
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)
* Set Variable Time1
* Set Variable Time2
**REQUIRES:**
1. **KM 8.2+**
2. **macOS 10.11.6 (El Capitan)**
TAGS: @Date @Time @JXA @Script
USER SETTINGS:
* Any Action in _magenta color_ is designed to be changed by end-user
ACTION COLOR CODES
* To facilitate the reading, customizing, and maintenance of this macro,
key Actions are colored as follows:
* GREEN -- Key Comments designed to highlight main sections of macro
* MAGENTA -- Actions designed to be customized by user
* YELLOW -- Primary Actions (usually the main purpose of the macro)
* ORANGE -- Actions that permanently destroy Variables or Clipboards,
OR IF/THEN and PAUSE Actions
**==USE AT YOUR OWN RISK==**
* While I have given this a modest amout of testing, and to the best of my knowledge will do no harm, I cannot guarantee it.
* If you have any doubts or questions:
* **Ask first**
* Turn on the KM Debugger from the KM Status Menu, and step through the macro, making sure you understand what it is doing with each Action.
---
![image|623x1174](upload://tRs53wGpzWlo1j7zdXyBu6JdzmA.png)
I really like the approach/script that Chris @ccstone uses in his macro.
It provides a much more flexible and reliable determination of the source date/time than most any other solution, including mine.
Chris @ccstone, I just started using this macro. Thanks for your efforts.
I track my sleeping and waking hours and minutes by recording the time I go to sleep and the time I go to bed. I use this macro to calculate the hours and minutes. In calculating my sleeping time there is a problem with when there is a PM time in local_Time01 and an AM time in local_Time02 that the answer comes out the remained of the 24-hour count rather than the correct answer.
I'd like to be able to "Prompt for User Input" all at once yesterday's wake time (4:32a) and last night's bedtime (8:39p) and this morning's wake time (4:39a) and "display results in a window" both differences in time. (16.07 and 8.00) These are both recorded in my journal at the same time each morning.
Can you give me some direction to modify this macro for my needs?
In order to do date math that goes across a day boundary, you will need to include the date with the time. So if you do NOT enter the date in your journal, then when you run the macro you will need to get todays date as a reference, and use it to prefix both times accordingly, then do the math.