Help on getting elapsed time and elapsed distance into correct formatting

Former Member
Former Member
Hi, I'm playing around with data fields and hoping someone can help with this.

Trying to get formatting for elapsed time into m:ss from the info.elapsedtime data. Would also like it to support over the hr mark and going h:mm

And for distance I'm trying to get info.elapseddistance into 0.00 format.

Is there any samples out there i can look at? Also need to know if I need to account for null values at the beginning of an activity etc.

Thank you
  • Here's a simple function that does the conversion for time (call with ms - etime=toHMS(info.elapsedTime/1000); ):

    function toHMS(secs) {
    var hr = secs/3600;
    var min = (secs-(hr*3600))/60;
    var sec = secs%60;
    if(hr!=0) {
    return hr.format("%d")+":"+min.format("%02d")+":"+sec.format("%02d");
    } else {
    return min.format("%02d")+":"+sec.format("%02d");
    }
    }



    Here's a simple one for distance ( call is elapsedDistance=toDist(info.elapsedDistance);, and you need to do the formatting on the distance - it's in kms or miles ):
    function toDist(value) {
    var ret=value/1000.0;
    if(Sys.getDeviceSettings().distanceUnits==Sys.UNIT_STATUTE) {
    ret=ret*0.621371;
    }
    return ret;
    }
  • Former Member
    Former Member over 8 years ago
    Here's a simple function that does the conversion for time (call with ms - etime=toHMS(info.elapsedTime/1000); ):

    function toHMS(secs) {
    var hr = secs/3600;
    var min = (secs-(hr*3600))/60;
    var sec = secs%60;
    if(hr!=0) {
    return hr.format("%d")+":"+min.format("%02d")+":"+sec.format("%02d");
    } else {
    return min.format("%02d")+":"+sec.format("%02d");
    }
    }



    Here's a simple one for distance ( call is elapsedDistance=toDist(info.elapsedDistance);, and you need to do the formatting on the distance - it's in kms or miles ):
    function toDist(value) {
    var ret=value/1000.0;
    if(Sys.getDeviceSettings().distanceUnits==Sys.UNIT_STATUTE) {
    ret=ret*0.621371;
    }
    return ret;
    }


    Thank you sir. Would you also have one for average speed by chance?

    Many thanks.
  • No problem (called with avgSpeed=toSpeed(info.averageSpeed); ) - value returned is mph or kph:

    function toSpeed(value) {
    var ret=(value*3600.0)/1000.0;
    if(Sys.getDeviceSettings().paceUnits==Sys.UNIT_STATUTE) { //you can use distanceUnits if you want
    ret=ret*0.621371;
    }
    return ret;
    }
  • Former Member
    Former Member over 8 years ago
    No problem (called with avgSpeed=toSpeed(info.averageSpeed); ) - value returned is mph or kph:

    function toSpeed(value) {
    var ret=(value*3600.0)/1000.0;
    if(Sys.getDeviceSettings().paceUnits==Sys.UNIT_STATUTE) { //you can use distanceUnits if you want
    ret=ret*0.621371;
    }
    return ret;
    }


    OK thanks. In terms of calling this, I'd just make avgspeed and elapseddistance a variable? I'm a bit confused on the elapsed time one though with 'call with ms - etime=toHMS(info.elapsedTime/1000); ):
  • OK thanks. In terms of calling this, I'd just make avgspeed and elapseddistance a variable? I'm a bit confused on the elapsed time one though with 'call with ms - etime=toHMS(info.elapsedTime/1000); ):


    Yes - I put the converted value in a variable. For the code I pasted, I'm doing this in compute() in a data field, and then in onUpdate(), I display the value. One thing I didn't show is you want to check things like info.elapsedDistance for null, as many things in "info" can be null when first starting.

    toHMS(info.elapsedTime/1000)
    info.elapsedTime is in milliseconds, so info.elapsedTime/1000 converts that into seconds