How to get total elapse time in hour and minutes.

I am working with dates can anyone know how to get difference of two event.timestamp(). In my form I am having Start button and Stop button. I want time elapse between two event.getTimeStamp() in format of HH:MM. How can I get the hours and minutes.

Any suggestions is appreciated.

Hi,

You could simply get the difference between the 2 dates in milliseconds and calculate the hours and minutes from that like so:

/**
 * @param {Date} eventStart
 * @param {Date} eventStop
 * @returns {String}
 */
function getElapsedTime(eventStart, eventStop) {
    // Get the milliseconds between the 2 events and calculate the hours and minutes
    var _nDiff = eventStop - eventStart,
        _nHours = Math.floor(_nDiff / 1000 / 60 / 60),
        _nMinutes = Math.floor((_nDiff - (_nHours * 60 * 60 * 1000)) / 1000 / 60);
    return _nHours + ":" + _nMinutes;
}

Hope this helps.

That’s quite an event if you measure it in hours and minutes instead of milliseconds! Looks like you could use some performance optimization :lol:

This works for me. I have it as a function in my database so any change to the two fields automatically updates the elapsed field.
Uses Dateutils plugin

function TimeTaken()
{

// Create an “empty” duration and set its fields later
var vDuration = plugins.DateUtils.Duration();
vDuration.days = 22;
vDuration.hours = 9;
vDuration.minutes = 12;
vDuration.seconds = 8;

var vlinstart = plugins.DateUtils.DateTime(linstart);
var vlinstop = plugins.DateUtils.DateTime(linstop);
vDuration = plugins.DateUtils.Duration(vlinstart,vlinstop);

return vDuration.hours + vDuration.minutes / 60
}
Regards
Mark

I actually prefer Roberts implementation because it doesn’t return decimals and it doesn’t rely on the optional dateUtils plugin (newcomers might not know it is not included with Servoy, feature request?).

Thank you all for detail discussion