KB Article #49128
Message Builder : How to calculate a Date difference in minutes
Problem
Resolution
How to calculate the difference between two date parameters in minutes ?
Resolution
In order to accomplish the above you need to write the following code:-
DECLARE FUNCTION DateDiff( $D1 PRIMITIVE, $D2 PRIMITIVE) PRIMITIVE
{
DECLARE VARIABLE $N1 INTEGER;
DECLARE VARIABLE $N2 INTEGER;
DECLARE VARIABLE $REZ INTEGER;
$N1 = $D1;
$N2 = $D2;
IF $N1 > $N2 {
$REZ = ($N1 - $N2 ) / 60;
}
ELSE {
$REZ = ($N2 - $N1 ) / 60;
}
RETURN $REZ;
}
Example:-
Test Code
DECLARE VARIABLE $x1 DATE;
DECLARE VARIABLE $x2 DATE;
$DateFormat="%Y-%m-%d %H:%M";
$x1 = "2007-09-14 08:16:00";
$x2 = "2007-09-15 23:59:00";
PRINT $x1 & " - " & $x2 & " = " & DateDiff($x2, $x1)& "\r\n";
The result will be:
2007-09-14 08:16 - 2007-09-15 23:59 = 2383