How to calculate a timecode from seconds?

How to calculate the seconds for example: 3552, to a time code?
The end result should be:
00:59:12

I am using the variables:

variabler
Tim1
Min1
Sek1

(being norwegian that made sense).
Tim1 = Hours
Min1 = Minutes
Sek1 = Seconds

Not sure how this fits into the other parts of your process, but if you wanted to use an Execute JS action, you could write something like this:

Timecode from seconds.kmmacros (18.9 KB)

(() => {
    'use strict';

    // timeCodeFromSeconds :: Int -> String
    const timeCodeFromSeconds = n => {
        const floor = Math.floor;
        return [
                floor(n / 3600),
                floor((n % 3600) / 60),
                n % 60
            ].map(x => x.toString().padStart(2, '0'))
            .join(':')
    }

    const main = () => {
        const kme = Application('Keyboard Maestro Engine');
        const
            strCode = timeCodeFromSeconds(
                kme.getvariable('totalSeconds')
            );
        return strCode;
    };

    // MAIN ---
    return main();
})();

1 Like

I wrote a JavaScript for Automation (JXA) script for this a few years ago that might help:

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

1 Like

Thanks! Looks nice, but it seems not to be working properly if the amounts of sec exceeds 1 hour. So for example 3771 is accordint to your script = 01:62:51, and it should have been 01:02:51

Thanks! I tested the script you wrote with the value 3771, but the initial results were:

NaN:NaN:NaN

Not sure what happened.

This is my intro:
script

I found a way.
Seems TIME__Decimals needed to be set to 0 in order to work.

Missing clause restored below : -)

(and in macro of original post now, too)

(() => {
    'use strict';

    // timeCodeFromSeconds :: Int -> String
    const timeCodeFromSeconds = n => {
        const floor = Math.floor;
        return [
                floor(n / 3600),
                floor((n % 3600) / 60),
                n % 60
            ].map(x => x.toString().padStart(2, '0'))
            .join(':')
    }

    const main = () => {
        const kme = Application('Keyboard Maestro Engine');
        const
            strCode = timeCodeFromSeconds(
                kme.getvariable('totalSeconds')
            );
        return strCode;
    };

    // MAIN ---
    return main();
})();
1 Like