# Get elapsed time between two timestamps

**URL:** <https://forum.servoy.com/t/get-elapsed-time-between-two-timestamps/3356>\
**Category:** Classic Servoy\
**Created:** [January 4, 2005, 7:37am UTC](https://forum.servoy.com/t/get-elapsed-time-between-two-timestamps/3356 "2005-01-04T07:37:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![friedrich\_toussaint](https://avatars.discourse-cdn.com/v4/letter/f/73ab20/32.png) [@friedrich\_toussaint](https://forum.servoy.com/u/friedrich_toussaint)\
**Post date:** [January 4, 2005, 7:37am UTC](https://forum.servoy.com/t/get-elapsed-time-between-two-timestamps/3356/1 "2005-01-04T07:37:21Z")

</div>

a happy new year to everybody,

with look for the causes of performance problems I found the posting of bcusic:  
[http://forum.servoy.com/viewtopic.php?t … ht=elapsed](http://forum.servoy.com/viewtopic.php?t=2135&highlight=elapsed)

the script works great, but i needed it somewhere more flexible: here is my version:

there are four arguments, syntax looks like this:

elements.result.txt = globals.getElapsed(globals.gStartDate, globals.gEndDate, ‘txt’, ‘DD’);  
elements.result.int = globals.getElapsed(globals.gStartDate, globals.gEndDate, ‘int’, ‘;’);  
elements.result.num = globals.getElapsed(globals.gStartDate, globals.gEndDate, ‘num’, ‘HH’);

the code:  
//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
// get arguments  
//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
var beg = arguments[0]; // start date  
var end = arguments[1]; // end date  
var ret\_type = arguments[2]; // the return data type  
var ret\_format = arguments[3]; // the return data format  
//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
/\*  
arg1 = startdate  
arg2 = enddate  
arg3 = return data type, ‘txt’, ‘int’ or ‘num’  
arg4 = return data fornat,  
at arg3 = ‘txt’ : the bigest returned format based on,  
e.g.: ‘HH’ returns ‘HH:mm:ss:SSS’ or  
‘DD’ returns ‘DD HH:mm:ss:SSS’ or  
‘ss’ returns ‘ss:SSS’  
at arg3 = ‘int’ : define ret\_format delimiter  
e.g: ‘;’ returns the array ‘DD;HH;mm;ss;ms’  
at arg3 = ‘num’ : define ret\_format you want to return,  
e.g.: ‘DD’, ‘HH’, ‘mm’, ‘ss’ or ‘SSS’  
_/  
//_\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
// calc difference (milliseconds)  
//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
var dif = Math.ceil(end - beg);  
//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
// calc returns number  
//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
var days = dif / 86400000;  
var hours = dif / 3600000;  
var minutes = dif / 60000;  
var seconds = dif / 1000;  
var milliseconds = dif / 1;  
//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
// calc returns int for can do DD HH-mm-ss-ms  
//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
var int\_days = Math.abs(dif / 86400000);  
var rest\_days = dif % 86400000;  
var int\_hours = Math.abs(rest\_days / 3600000);  
var rest\_hours = rest\_days % 3600000;  
var int\_minutes = Math.abs(rest\_hours /60000);  
var rest\_minutes = rest\_hours % 60000;  
var int\_seconds = Math.abs(rest\_minutes / 1000);  
var rest\_seconds = rest\_minutes % 1000;  
//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
// calc return num/txt for can do DD HH-mm-ss-SSS  
//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
if (days == 0)  
{  
var numD = ‘000’;  
}  
else if (days \< 10)  
{  
var numD = ‘00’ + utils.numberFormat(Math.floor(days), 0);  
}  
else if (days \< 100)  
{  
var numD = ‘0’ + utils.numberFormat(Math.floor(days), 0);  
}  
else  
{  
var numD = utils.numberFormat(Math.floor(days), 0);  
}  
if (hours == 0)  
{  
var numH = ‘00’;  
}  
else if (hours \< 10)  
{  
var numH = ‘0’ + utils.numberFormat(Math.floor(hours), 0);  
}  
else  
{  
var numH = utils.numberFormat(Math.floor(hours), 0);  
}  
if (minutes == 0)  
{  
var numM = ‘00’;  
}  
else if (minutes \< 10)  
{  
var numM = ‘0’ + utils.numberFormat(Math.floor(minutes), 0);  
}  
else  
{  
var numM = utils.numberFormat(Math.floor(minutes), 0);  
}  
if (seconds == 0)  
{  
var numS = ‘00’;  
}  
else if (seconds \< 10)  
{  
var numS = ‘0’ + utils.numberFormat(Math.floor(seconds), 0);  
}  
else  
{  
var numS = utils.numberFormat(Math.floor(seconds), 0);  
}  
if (milliseconds == 0)  
{  
var numMS = ‘0000’;  
}  
else if (milliseconds \< 10)  
{  
var numMS = ‘00’ + utils.numberFormat(milliseconds, 0);  
}  
else if (milliseconds \< 100)  
{  
var numMS = ‘0’ + utils.numberFormat(milliseconds, 0);  
}  
else  
{  
var numMS = utils.numberFormat(milliseconds, 0);  
}  
//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
// calc return int/txt for can do DD HH-mm-ss-SSS  
//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
if (int\_days == 0)  
{  
var intD = ‘000’;  
}  
else if (int\_days \< 10)  
{  
var intD = ‘00’ + utils.numberFormat(Math.floor(int\_days), 0);  
}  
else if (days \< 100)  
{  
var intD = ‘0’ + utils.numberFormat(Math.floor(int\_days), 0);  
}  
else  
{  
var intD = utils.numberFormat(Math.floor(int\_days), 0);  
}  
if (int\_hours == 0)  
{  
var intH = ‘00’;  
}  
else if (int\_hours \< 10)  
{  
var intH = ‘0’ + utils.numberFormat(Math.floor(int\_hours), 0);  
}  
else  
{  
var intH = utils.numberFormat(Math.floor(int\_hours), 0);  
}  
if (int\_minutes == 0)  
{  
var intM = ‘00’;  
}  
else if (int\_minutes \< 10)  
{  
var intM = ‘0’ + utils.numberFormat(Math.floor(int\_minutes), 0);  
}  
else  
{  
var intM = utils.numberFormat(Math.floor(int\_minutes), 0);  
}  
if (int\_seconds == 0)  
{  
var intS = ‘00’;  
}  
else if (int\_seconds \< 10)  
{  
var intS = ‘0’ + utils.numberFormat(Math.floor(int\_seconds), 0);  
}  
else  
{  
var intS = utils.numberFormat(Math.floor(int\_seconds), 0);  
}  
if (rest\_seconds == 0)  
{  
var intMS = ‘000’;  
}  
else if (rest\_seconds \< 10)  
{  
var intMS = ‘00’ + utils.numberFormat(rest\_seconds, 0);  
}  
else if (rest\_seconds \< 100)  
{  
var intMS = ‘0’ + utils.numberFormat(rest\_seconds, 0);  
}  
else  
{  
var intMS = utils.numberFormat(rest\_seconds, 0);  
}  
//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
// check ret\_type  
//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
switch( ret\_type )  
{  
case ‘txt’ : if (!ret\_format)  
{  
plugins.dialogs.showErrorDialog( ‘Error’, ‘err#0002: type txt, no return format defined!’, ‘OK’);  
}  
else  
{  
switch(ret\_format)  
{  
case ‘DD’ : var ret\_calc = ‘DD HH:mm:ss,SSS’;  
ret\_calc = utils.stringReplace(ret\_calc, ‘DD’, numD);  
ret\_calc = utils.stringReplace(ret\_calc, ‘HH’, intH);  
ret\_calc = utils.stringReplace(ret\_calc, ‘mm’, intM);  
ret\_calc = utils.stringReplace(ret\_calc, ‘ss’, intS);  
ret\_calc = utils.stringReplace(ret\_calc, ‘SSS’, intMS);  
break;  
case ‘HH’ : var ret\_calc = ‘HH:mm:ss:SSS’;  
ret\_calc = utils.stringReplace(ret\_calc, ‘HH’, numH);  
ret\_calc = utils.stringReplace(ret\_calc, ‘mm’, intM);  
ret\_calc = utils.stringReplace(ret\_calc, ‘ss’, intS);  
ret\_calc = utils.stringReplace(ret\_calc, ‘SSS’, intMS);  
break;  
case ‘mm’ : var ret\_calc = ‘mm:ss:SSS’;  
ret\_calc = utils.stringReplace(ret\_calc, ‘mm’, numM);  
ret\_calc = utils.stringReplace(ret\_calc, ‘ss’, intS);  
ret\_calc = utils.stringReplace(ret\_calc, ‘SSS’, intMS);  
break;  
case ‘ss’ : var ret\_calc = ‘ss:SSS’;  
ret\_calc = utils.stringReplace(ret\_calc, ‘ss’, numS);  
ret\_calc = utils.stringReplace(ret\_calc, ‘SSS’, intMS);  
break;  
case ‘SSS’ : var ret\_calc = ‘SSS’;  
ret\_calc = utils.stringReplace(ret\_calc, ‘SSS’, numMS);  
break;  
default : plugins.dialogs.showErrorDialog( ‘Error’, ‘err#0002: type txt, wrong return format defined!’, ‘OK’);  
}  
}  
break;  
case ‘int’ :if (!ret\_format)  
{  
plugins.dialogs.showErrorDialog( ‘Error’, ‘err#0003: type int, no return format defined!’, ‘OK’);  
}  
else  
{  
var ret\_calc = ‘DD’ + ret\_format + ‘HH’ + ret\_format + ‘mm’ + ret\_format + ‘ss’ + ret\_format + ‘SSS’;  
ret\_calc = utils.stringReplace(ret\_calc, ‘DD’, utils.numberFormat(int\_days, 0));  
ret\_calc = utils.stringReplace(ret\_calc, ‘HH’, utils.numberFormat(int\_hours, 0));  
ret\_calc = utils.stringReplace(ret\_calc, ‘mm’, utils.numberFormat(int\_minutes, 0));  
ret\_calc = utils.stringReplace(ret\_calc, ‘ss’, utils.numberFormat(int\_seconds, 0));  
ret\_calc = utils.stringReplace(ret\_calc, ‘SSS’, utils.numberFormat(rest\_seconds, 0));  
}  
break;  
case ‘num’ : var ret\_array = ret\_format.split(‘,’,2);  
switch( ret\_array[0])  
{  
case ‘DD’ : var ret\_calc = days;  
break;  
case ‘HH’ : var ret\_calc = hours;  
break;  
case ‘mm’ : var ret\_calc = minutes;  
break;  
case ‘ss’ : var ret\_calc = seconds;  
break;  
case ‘SSS’ : var ret\_calc = milliseconds;  
break;  
default : plugins.dialogs.showErrorDialog( ‘Error’, ‘err#0004: type num, no or wrong return format defined!’, ‘OK’);  
}  
break;  
default: plugins.dialogs.showErrorDialog( ‘Error’, ‘err#0001: no return type defined!’, ‘OK’);  
}  
//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
// return calculation  
//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
return ret\_calc;  
//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

have a nice day  
your friedrich toussaint

---

<div class="post-metadata">

**Author:** ![bcusick](https://avatars.discourse-cdn.com/v4/letter/b/ed8c4c/32.png) [@bcusick](https://forum.servoy.com/u/bcusick)\
**Post date:** [January 5, 2005, 9:29pm UTC](https://forum.servoy.com/t/get-elapsed-time-between-two-timestamps/3356/2 "2005-01-05T21:29:32Z")

</div>

Friedrich,

Thanks for sharing this!

Nice work!
