Casting Moment Object

I'm trying to add time from the starting time of the app :
// in the compute method :
var myNewHour = getNewHour(info.startTime);

function getNewHour(startTime)
var timeToAdd = 12;

var newHour = startTime.value().toNumber() + timeToAdd; //This is there that I have my problem, I don't know how to do it
System.println("newHour = "+newHour);

// The end of the method for info
//formating
var s = newHour.toLong();
var hours = s / 3600;
s -= hours * 3600;
var minutes = s / 60;
s -= minutes * 60;
var fmt;
if (hours >= 10) {
fmt = "" + hours + ":" + minutes.format("%02d");
}else if(hours > 0 && hours <10 ){
fmt = "0" + hours + ":" + minutes.format("%02d");
}
else {
fmt = "00:" + minutes.format("%02d") + ":" + s.format("%02d");
}
return fmt;

  • Well, info.startTime is a Time.Moment. If you want to add to it, you can call the Moment.add() method, or you can convert to a Lang.Number via Moment.value() and use that.

    As your code is right now, your code takes the activity start time (number of seconds since some epoch value like 1990-01-01 00:00:00), adds 12 seconds to it, and then breaks that into hours, minutes, and seconds. Given that we're looking at seconds since the epoch, the hours value is going to be huge, and I'm guessing this is not what you want.

    What is it that you are trying to do?
  • In fact in the code I put.

    If I put info.elapsedTime instead of info.startTime, this is exactly what I wanted to have.

    but the problem is that the info.elapsedTime isn't the startTime and continue to grow up. I wanted to have the startTime :
    For example :
    the starTime is 21:00 , I add 3 mns , my function must return 21:03 during everytime.
  • Sorry, I've been writing and reading code for years; your code is not very self explanatory.

    Given that you are formatting the time value differently based on the hour, it made sense that you were probably trying to format a duration. It doesn't make any sense to me for the midnight hour to appear as minutes and seconds and then for all other hours of the day as hours and minutes. I assumed that the code you omitted was generating a duration that you were then formatting.

    // extract the hours/minutes/seconds from a moment and format them as
    //
    // HH:MM:SS for times between midnight and 1am
    //
    // and
    //
    // HH:MM for everything else
    //
    // I've got no idea why you'd want to do this, but this is what your code was doing.
    //
    function format_time(moment) {

    // if you use `moment.value()' you will get a time in the UTC time zone
    var timestamp = moment.value();

    // the result will be offset from your local time by the DST offset in
    // effect for your time zone. so we compensate for that
    timestamp += Sys.getClockTime().timeZoneOffset;

    // drop off anything more than 24 hours
    timestamp %= 86400;

    // convert to hours, minutes and seconds
    var hh = timestamp / 3600;
    var mm = (timestamp / 60) % 60;
    var ss = timestamp % 60;

    //
    // you could also use the following this to just ask the system to break it down
    // for you. it should have the same effect as the above.
    //
    // moment = moment.add(Sys.getClockTime().timeZoneOffset);
    //
    // ask the system to break out the date and time values. this would be the
    // preferred method if you want to display the day, month, or year.
    //
    // var info = Gregorian.info(moment, Time.FORMAT_SHORT);
    // var hh = info.hour;
    // var mm = info.min;
    // var ss = info.sec;

    if (hh < 1) {
    return Lang.format("00:$1$:$2$", [
    mm.format("%02d"),
    ss.format("%02d")
    ]);

    // return "00:" + mm.format("%02d") + ":" + ss.format("%02d");
    }
    else {
    return Lang.format("$1$:$2$", [
    hh.format("%02d"),
    mm.format("%02d")
    ]);

    // return hh.format("%02d") + ":" + mm.format("%02d");
    }
    }

    function get_moment_12h_forward(moment) {
    return moment.add(new Time.Duration(Time.SECONDS_PER_HOUR * 12)); // advance 12 hours into the future
    }


    I'd use it like this...

    var s = format_time(get_moment_12h_forward(moment));


    Additionally, since the value is not changing, you should only need to call this function once and cache the result.
  • Sorry, I believed it was easy to understand :
    function getNewTime(moment, delay) {

    return formated (moment + delay); //hh:mm (not ss)
    }


    moment is fixed (the info.startTime) , but delay could change

    With your code, I think that I'll be able to manage it. Thanks