Adding minutes to a custom time (NOT current time)

I'd like to add minutes to a custom time.

For example:
custom time = "9:00"

then "add 150 minutes" to the custom time

result is: "11:30"

Who knows how to do this?

Split to hours and minutes on the ":".
Multiply hours by 60, add minutes, add your offset in minutes.
Set new hours to ((total minutes div 60) mod 24).
Set new minutes to total minutes mod 60.
Pad each with a leading "0" then trim to the rightmost 2 characters.
Concat new hours & ":" & new minutes.

Sorry, not at a KM machine at the moment so don't know if there are any built-ins to make this easier -- but the above might tide you over until the Cool Kids chime in with a proper solution!

@Nige_S's solution is the straightforward way. If you enjoy exploring the corners of the Unix command line world, try using the date command with the -v option, as in this macro.

Added Time Test.kmmacros (2.4 KB)

2 Likes

I'm still looking for a "KM native" solution using time functions, but here's an AppleScript version similar to yours:

AS Add Minutes.kmmacros (4.3 KB)

1 Like

And here it is. Frankly -- it's horrible, and perhaps best used of an example of why it's sometimes better to use "external" functions! I'm sure there's a cleaner way to do this within KM itself but, try as I might, I couldn't come up with a good way of creating an arbitrary "time object" to do the maths on.

timeTest.kmmacros (4.6 KB)

Summary

1 Like