How to get total elapse time in hour and minutes.

Find out how to get things done with Servoy. Post how YOU get things done with Servoy

How to get total elapse time in hour and minutes.

Postby hardina09 » Mon Jun 25, 2012 10:49 pm

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.
hardina09
 
Posts: 62
Joined: Tue Apr 24, 2012 9:46 pm

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

Postby ROCLASI » Tue Jun 26, 2012 12:40 am

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.
Robert Ivens
ROCLASI Software Solutions / JBS Group, Partner
SAN Developer / Servoy Valued Professional / Servoy Certified Developer
Twitter: @roclasi / @servoyforge
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5205
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

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

Postby omar » Tue Jun 26, 2012 10:48 am

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:
Intrasoft / JBS Group, Partner
Omar van Galen
Servoy Developer
omar
 
Posts: 316
Joined: Sat Feb 12, 2011 4:51 pm
Location: Intrasoft, The Netherlands

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

Postby Mark Crichton » Tue Jun 26, 2012 11:49 am

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
Mark Crichton
DataWise Ltd - New Zealand
http://www.datawise.co.nz
User avatar
Mark Crichton
 
Posts: 109
Joined: Mon Jan 24, 2011 10:01 am
Location: Auckland, New Zealand

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

Postby omar » Tue Jun 26, 2012 12:42 pm

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?).
Intrasoft / JBS Group, Partner
Omar van Galen
Servoy Developer
omar
 
Posts: 316
Joined: Sat Feb 12, 2011 4:51 pm
Location: Intrasoft, The Netherlands

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

Postby hardina09 » Tue Jun 26, 2012 7:33 pm

Thank you all for detail discussion
hardina09
 
Posts: 62
Joined: Tue Apr 24, 2012 9:46 pm


Return to How To

Who is online

Users browsing this forum: No registered users and 1 guest