Page 1 of 1

How to get total elapse time in hour and minutes.

PostPosted: Mon Jun 25, 2012 10:49 pm
by hardina09
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.

Re: How to get total elapse time in hour and minutes.

PostPosted: Tue Jun 26, 2012 12:40 am
by ROCLASI
Hi,

You could simply get the difference between the 2 dates in milliseconds and calculate the hours and minutes from that like so:
Code: Select all
/**
* @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.

Re: How to get total elapse time in hour and minutes.

PostPosted: Tue Jun 26, 2012 10:48 am
by omar
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:

Re: How to get total elapse time in hour and minutes.

PostPosted: Tue Jun 26, 2012 11:49 am
by Mark Crichton
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

Re: How to get total elapse time in hour and minutes.

PostPosted: Tue Jun 26, 2012 12:42 pm
by omar
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?).

Re: How to get total elapse time in hour and minutes.

PostPosted: Tue Jun 26, 2012 7:33 pm
by hardina09
Thank you all for detail discussion