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.
  • 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.


    Seems like u can use either for this countdown timer(?)

    1) calendar.duration and use add/subtract
    2) convert all the numbers to seconds and then do the subtracting and then perform formatting to hh:mm:ss

    In my app, my 2nd ever app that took me like 2-3weeks to finish, I was using Sys.gettimer() exclusively. It was a HUGE pain.
  • I want to subtract #1 from #2, and then to display the result in the format hh:mm:ss.

    This is what you claim you are trying to do... Assume that info.startTime is an absolute time (Moment), like 7:32am. You want to add a relative time (a time offset/Duration) of 10h 30m to that. This will get you an absolute time again (5:32pm in our example so far). Then you want to subtract the elapsed time from that, which will give you back an absolute time again. If you had started at 7:32am and ran for 2 minutes, the result would be 5:30pm. I'm not sure this is what you want. It doesn't make any sense to me.

    Are you just trying to calculate the offset from a goal time of 10h 30m? If so, this could be more easily expressed as remainingTime = goalTime - elapsedTime. The code for that is not very complicated.

    var goalTimeSec = 10 * Cal.SECONDS_PER_HOUR + 30 * Cal.SECONDS_PER_MINUTE;
    var elapsedTimeSec = info.elapsedTime / 1000;

    var deltaTimeSec = 0;
    if (elapsedTimeSec < goalTimeSec) {
    deltaTimeSec = goalTimeSec - elapsedTimeSec;
    }


    If you want to display the time beyond your goal time, instead of 0:00, you could write...

    var deltaTimeSec;
    if (elapsedTimeSec < goalTime) {
    deltaTimeSec = goalTimeSec - elapsedTimeSec;
    }
    else {
    deltaTimeSec = elapsedTimeSec - goalTimeSec;
    }


    The only remaining thing to do is to format the time delta as hours/minutes/seconds. This is typically where people run into problems.

    function format_duration(seconds) {
    var hh = seconds / 3600;
    var mm = seconds / 60 % 60;
    var ss = seconds % 60;

    if (hh != 0) {
    return Lang.format("$1$:$2$:$3$", [
    hh,
    mm.format("%02d"),
    ss.format("%02d")
    ]);
    }
    else {
    return Lang.format("$1$:$2$", [
    mm,
    ss.format("%02d")
    ]);
    }
    }
  • Former Member
    Former Member
    Are you just trying to calculate the offset from a goal time of 10h 30m? If so, this could be more easily expressed as remainingTime = goalTime - elapsedTime.


    Yes, you are correct....clearly I was in a downwards spiral of confusion last night :-)

    Thank you very much for your assistance (both of you). Very much appreciated. Now I will be able to finish my app! I am not an experienced programmer, but I thought of an app idea that will be helpful to me during next month's Ultra Trail du Mont Blanc foot race. I've already built a simple data field to use during training for the race, but this new one will be put to use during the run itself, telling me the name of the next Checkpoint (town), the distance to go to the Checkpoint, how much more vertical gain to the Checkpoint, and (thanks to your assistance!) how much time remains until the cut-off at that Checkpoint.

    Thank you again!

    Cheers,
    James.
  • James,

    I would like to do the same for some of the UK ultra courses. Is there any chance you could share your code with me, to save me having to re-write a load of stuff you will already have done? I won't monetize any app I make with this code.

    Thanks,
    Phil
  • Former Member
    Former Member
    Hi Phil,

    Yeah, I think that would be fine. We could do it a couple of different ways I guess:

    1. Let me know the details of the events you were thinking of, and I will build and release the corresponding Datafields to the app store.

    2. I send you the code and you create the Datafields yourself.

    3. We set up a shared repository on Github, post the Datafield code in there, and work from there.

    I think that this type of Datafield has a lot of potential for improvement, so I am very keen to share the code and hopefully see others take it further. For example, I would love to see all of the different values displayed in a single field layout at the same time.

    Please let me know your thoughts.

    Cheers,
    James.
  • Former Member
    Former Member
    Hello James and Phil,

    I am very interested in this project but I know nothing about apps.

    What device will this app run on?

    I make gps tracks and maps for 25, 50, 75 and 100 mile horse endurance races.
    There are fixed checkpoints (not equal distances and different for each race) with fixed hold times and fixed cutoff times and a fixed total time allowed to complete.

    Currently I make the track at an average of 6mph then jump the timestamps ahead the amount of hold time at each checkpoint. This gives the average rider a timeline that the average well conditioned horse can meet and easily finish the race with time to spare.

    My crude timeline does not work for those who want to actually race for Winning or Top Ten.

    Your app sounds like it would be a great timing tool for those who want to race so that is why I am interested.
  • Former Member
    Former Member
    Hi Don,

    Firstly, your events sound like a heck of a lot of fun, good on you for organising them!

    The app in its current state is just a Datafield that compares the user's current progress against a predefined set of distance, time, ascent, and checkpoint name information. It does not record the user's time through each checkpoint - it just lets them know information about the next checkpoint that they are approaching.

    If I understand correctly, you are after something that could record progress through each checkpoint, not just compare the user's progress through the course. Have I got that right?

    Cheers,
    James.
  • Former Member
    Former Member
    Hello James,

    Yes that is exactly what I would like to find. Something fluid that would use time and distance covered but not use course navigation. I have tried that several times and it is just too prone to gps failure. I use an Edge705 with a GPX track of the race loaded, hit START, lock the keys and glance at it when in doubt about which way to go. That has worked very well for several years to keep me on track thru the woods. :)
  • Former Member
    Former Member
    The only remaining thing to do is to format the time delta as hours/minutes/seconds. This is typically where people run into problems.

    function format_duration(seconds) {
    var hh = seconds / 3600;
    var mm = seconds / 60 % 60;
    var ss = seconds % 60;

    if (hh != 0) {
    return Lang.format("$1$:$2$:$3$", [
    hh,
    mm.format("%02d"),
    ss.format("%02d")
    ]);
    }
    else {
    return Lang.format("$1$:$2$", [
    mm,
    ss.format("%02d")
    ]);
    }
    }


    Sorry to necro this thread. Just thought I'd say thanks for sharing this code, Travis! This sort of thing would be very useful in the SDK samples.

    Another thought, maybe there could be a sticky thread where reusable functions like this could be shared?
  • Former Member
    Former Member
    Hey,

    That's no problem and I like the idea of a common place for people to share some reusable functions. Why not start one? If it starts to catch on it's something I would totally consider sticking!

    Thanks,
    -Coleman