How to calculate the number of days between two dates in BusinessWorks and BusinessWorks Container Edition
It is possible to calculate a duration between two dates in BusinessWorks but there are a few things to know that are explained in this article.
The duration between two dates is calculated by just doing a subtraction between the two date objects but the result is an object of type duration and there is then need to use an XPATH function to convert the duration object to a number of days.
The calculation of the duration between two dates can be done with the following:
$Mapper/d1 — $Mapper/d2
Then the result can be converted to a number of days using the following function:
days-from-duration()
In the example mapping below:
· The local variable ‘$duration’ is set with the duration between the two dates d1 and d2
· The output field ‘du’ of type duration is set with the duration between the two dates d1 and d2
· The output field ‘ddiff’ of type integer is set with the number of days between the two dates d1 and d2
There are also XPATH functions to calculate the number of hours, minutes or seconds between two dateTime objects or two times objects:
. hours-from-duration()
. minutes-from-duration()
. seconds-from-duration()
But it is important to know that each of them will only calculate the difference for the unit specified without taking into account other units of time.
For example if you use the function minutes-from-duration() to calculate the number of minutes between 02:27:56 and 01:23:18 it will return 4.
To calculate the number of minutes between two times you then need to calculate the number of hours, multiply by 60, and then calculate the number of minutes and add the two numbers.
The formula above returns the correct value that is 64.
The same principles should be followed to calculate the number of hours, minutes or seconds between two dateTime objects.
Additional elements: