Newbie question on Time calculation

Former Member
Former Member
Hi all,

I have a newbie question relating to calculations between two time values.

I want to perform a calculation between two values in a DataField:

1. The info.elapsedTime value, and
2. (The info.startTime value + 10 hours and thirty minutes).

I want to subtract #1 from #2, and then to display the result in the format hh:mm:ss.

I have been struggling with this for a while and just can't get it to work. Can somebody please help me out?

Cheers,
James.
  • one thing to remember in MonkeyC is that you ought to try to write as compact code as possible as every line of code consumes memory.

    in the case of travis' function an easy improvement is to use the ? operator instead of the explicit if.
    function format_duration(seconds) {
    var hh = seconds / 3600;
    var mm = seconds / 60 % 60;
    var ss = seconds % 60;

    return Lang.format(hh>0?"$1$:$2$:$3$":"$2$:$3$",[hh, mm.format("%02d"), ss.format("%02d")]);
    }
  • I've been collecting threads like this to eventually put somewhere. I just haven't figured out what will work best (and there's higher priority stuff on my plate at the moment). My current thought is to have a section on the developer site with useful snippets and techniques, but we've also talked about putting reusable code on github. There are things in the works that will definitely help out with this.

    I thought I'd also point out that, while we don't have this specific example respresented anywhere, we do have similar examples in the API docs now:

    https://developer.garmin.com/downloads/connect-iq/monkey-c/doc/Toybox/Lang.html
  • Former Member
    Former Member
    one thing to remember in MonkeyC is that you ought to try to write as compact code as possible as every line of code consumes memory.

    in the case of travis' function an easy improvement is to use the ? operator instead of the explicit if.
    function format_duration(seconds) {
    var hh = seconds / 3600;
    var mm = seconds / 60 % 60;
    var ss = seconds % 60;

    return Lang.format(hh>0?"$1$:$2$:$3$":"$2$:$3$",[hh, mm.format("%02d"), ss.format("%02d")]);
    }


    Awesome. Thanks for sharing that, Peter.

    A sub-forum for re-useable code would make it easier to find suitable code. Having a thread for each would enable people to optimize/improve the code, like what Peter has done here.

    Or alternatively, but more manual, someone creates and maintains a sticky thread with links to code snippet threads.
  • Former Member
    Former Member
    Another great example is Petr's reply in this thread:
    https://forums.garmin.com/showthread.php?319140-How-to-detect-which-device-my-app-is-running-on&p=709418#post709418

    I think that currently only "simple" solution is to make string resource for each device type in which you define some string, i.e. deviceName.



    And then, in the constructor or in onLayout() method you can load this string..

    var deviceName = Ui.loadResource(Rez.Strings.deviceName);
    if (!deviceName.equals("fenix3") && !deviceName.equals("d2bravo")) {
    // do something
    }


    This one should be a FAQ.
  • Actually, that one is a bit dated (it was back in the 1.1.x CIQ days). There are different options today with things like build excludes, and other tricks to determine what you need to know about the device you are on.