Xplan Quick Xplain
Datediff: difference between dates made easy
A quick but handy function to keep in mind for
In our date coding article one of the things we looked at was how you can calculate the difference between two date periods. Usually we might need to do this for fact finds, advice documents and coded application forms, common examples include:
- Calculate retirement ages or age at retirement;
- Calculate difference between x age and y legislative age;
- Exclude or include residence and employment based on x years (application forms)
Datediff() makes the calculations simpler
Datediff (Date difference) takes two date objects and works out the difference in years, months and days between the two:
x = Date item 1
y = Date item 2
z = .years or .month or .day
Because of the reverse countdown aspect of it, .years will be the main measure to use. Some examples of its use:
Years till retirement: <:=datediff($client.retire_date, datetime_merge).years:>
Retirement age: <:=datediff($client.retire_date, $client.dob).years:>
Note: Sometimes its handy to know work arounds because automated functions in xplan can go wrong. For example the retirement age shown on screen in 2.14.14 versions is +1 year off. If we were using an automated function to get the retirement age (similar to $client.age) our documents would be wrong until IRESS patches this. Clients don’t or can’t always wait for that to happen, particularly on large sites were even small versions go through internal testing so knowing alternative methods of getting the data can be invaluable.
Time at current residence:
<:if int(datediff($client.date_moved_in, datetime_merge).years) <3:>
Please provide details of previous residency
Keep in mind:
- Datediff works on a reverse countdown so if you were working out the points between 2 dates eg (01/01/1980 and 01/01/2015) the result returned would be 3 years, 0 months 0 days.
- Datediff runs of years, months, days note the plural of the s this is in contrast to when we’re dealing with a single date and to get the individual values its datetime_merge.year etc.