# Less-than-10 MOD formatting puzzle

I have a formatting problem that occurs in one of the steps of a small KM macro I have for converting minutes into hours and minutes. Here’s an example of the text that I have, and how it turns out. First, here’s what I have:

Very active (65), fairly active (5), lightly active (267) and sedentary (515).

After applying the macro, which just copies the number of minutes and then applies the Applescript, which then pastes in the conversion and replaces each bracketed number of minutes. The Applescript step is:

set answer to ((the clipboard) div 60 as string) & “:” & (the clipboard) mod 60

I get this conversion:

Very active (1:5), fairly active (0:5), lightly active (4:27) and sedentary (8:35)

In other words, the conversion works fine unless the number of minutes is less than 10 in the answer, as in 1:5 which should be 1:05, either in hours:minutes or just minutes, when the result of (5) should convert to (0:05).

What should I do to fix the Applescript the less-than-10 formatting problem?

I just posted an update to this plugin:

I believe it will do what you want. Let me know if you need help with it.

If the value is < 10, then you need to Left Pad with "0".

As an alternate to using @DanThomas' great Plugin, here is a JavaScript that converts seconds to HH:MM:SS. You could easily mod to output only HH:MM.

### Script: Convert Seconds to HH:MM:SS<.nnn>

Seconds can be formatted to 0 or n decimals

Requires the KM Variable "TIME__ElapsedSeconds" to be set in KM Macro before calling script. Also, a KM Variable "TIME__Decimals" can be set to the number decimals to show in the seconds (default is 0).

#### Example Results

``````08:25:07
08:25:06.63
``````

#### JavaScript

``````'use strict';
(function () {    // Function is auto-run when script is executed
/*

---

Convert seconds to HH:MM:SS.nnn

---

DATE:    2016-12-02
AUTHOR: @JMichaelTX
REF:
• Convert seconds to HH-MM-SS with JavaScript? - Stack Overflow
• stackoverflow.com, general
• http://stackoverflow.com/a/38467919/915019

---

*/
// --- GET DATA FROM KM VARIABLES ---

var kmeApp = Application("Keyboard Maestro Engine");

// -- DEFAULT VALUES APPEAR AFTER THE "||" --
var elapsedSeconds   = kmeApp.getvariable('TIME__ElapsedSeconds');
var decimals         = kmeApp.getvariable('TIME__Decimals') || 0;

// --- BREAKDOWN SECONS INTO HOURS, MINUTES, SECONDS ---

var hours = parseInt( elapsedSeconds / 3600 ) % 24;
var minutes = parseInt( elapsedSeconds / 60 ) % 60;
var seconds = elapsedSeconds % 60;

// --- ROUND SECONDS TO REQUIRED DECIMALS ---
seconds = round(seconds,decimals);

// --- BUILD TIME STRING, PADDED WITH "0" ---
var timeStr = (hours < 10 ? "0" + hours : hours)
+ ":" + (minutes < 10 ? "0" + minutes : minutes)
+ ":" + (seconds  < 10 ? "0" + seconds : seconds);

return timeStr

//~~~~~~~~~~~~~ END OF MAIN SCRIPT ~~~~~~~~~~~~~~~~~~~~~~~~~~~

// --- ROUND USING ACCURATE METHOD ---
function round(value, decimals) {
return Number(Math.round(value+'e'+decimals)+'e-'+decimals);
}

})();
``````

Assuming you have the expression in Keyboard Maestro, you can use Search & Replace to add the 0s in. Since they are all of the form ":" digit ")", the pattern is pretty easy to detect.

1 Like

The script looks good, but why would it yield sometimes "08:25:06.63"
My needs personally are "08:25:06", no more parameters for me other than that please

Did you read the notes above the script?

1 Like

Thanks for the help, Peter. That works well!

Thanks for the help, JMichaelTX. That also works well!

Thanks, Dan. This intrigues me. I have some other time issues, and this should work well.