New to programming help getting time format right for pace and total time

Former Member
Former Member
I am having my first stab at programming ready for when I get a new garmin. I want to create a data field which shows the estimated finish time for a set distance such as 5km, 10km, half marathon and full marathon based on average speed. So far I have created it for 10km, and the total time for 10km is shown as 69.50 instead of 01:09:30 please can you show me how I would do this:confused:. The code so far is:

using Toybox.WatchUi as Ui;

class testdatafieldView extends Ui.SimpleDataField {

//! Set the label of the data field here.
function initialize() {
label = "10km time";
}

//! The given info object contains all the current workout
//! information. Calculate a value and return it in this method.
function compute(info) {
// See Activity.Info in the documentation for available information.
var tenkmtimesecs = null;
var tenkmtimemins = null;
var distanceleft = null;
var timeleftsecs = null;
var timeleftmins = null;
// if( info.elapsedDistance <= 10000 )
// {
// Find out how much distance there is to go:
distanceleft = (10000 - info.elapsedDistance);
// Take the distance left and divide by the average speed:
timeleftsecs = (distanceleft / info.averageSpeed);
// convert from seconds to minutes
timeleftmins = (timeleftsecs / 60 );
// Add the timeleft in seconds to the elsapsed time in milliseconds
tenkmtimesecs = ((info.elapsedTime / 1000) + timeleftsecs);
// Convert tenkmtime from seconds to minutes
tenkmtimemins = (tenkmtimesecs / 60 );
// }
// return distanceleft;
//return pace;
return tenkmtimemins;
}

}



Once I have worked it out once I then want to create a view or widget of the four data fields on one screen but I think I am a while away from doing that. I also need to work out when distance reaches 10km to hold that time.

Any help appreciated.:cool:
  • // t = (h * 3600000) + (m * 60000) + (s * 1000) + ms;

    var time_in_milliseconds = info.elapsedTime;

    var hrs = time_in_milliseconds / 3600000;
    time_in_milliseconds -= (hrs * 3600000);

    var min = time_in_milliseconds / 60000;
    time_in_milliseconds -= (min * 60000);

    var sec = time_in_milliseconds / 1000;
    time_in_milliseconds -= (sec * 1000);

    var msec = time_in_milliseconds;

    var s = Lang.format("$1$ = $2$:$3$:$4$.$5$",
    [ info.elapsedTime, hrs, min.format("%0.2d"), sec.format("%0.2d"), msec.format("%0.3d") ]);


    Note that you don't have to create a widget to get each of your fields on the screen at once. If you just make four separate simple data fields, the user should be able to select each of them and put them on the same screen if that is what they want.

    Also, when you post code snippets, try to use [ code] and [ /code] tags around them so that it is formatted properly.