Calculating Date Differences

The dateDiff() calculates the difference in days between two dates

The generic form is date2(yyyy,mm,dd).dateDiff(date1(yyyy,mm,dd))

Note the capitalised 'D' in dateDiff

As an example to calculate the number of days until Christmas from today (10th of January 2021)

date(2022, 12, 25).dateDiff(date(2022, 1, 10))

image

Instead of typing in today’s date, you could alternatively use today() as follows

date(2022,12,25).dateDiff(today())

This is the same as using the age() function in the expression date(2015,12,21).age()

Except where .dateDiff(today()) defaults to the number of days until the given dates, whereas .age() defaults to the number of years that the given date is after today

date2.dateDiff(date1) calculates number of days from date 1 to date 2

date(2022, 12, 25).dateDiff(date(2022, 1, 10)) returns 349 (days)

If you swap the 2 dates, you get '-349' – so ensure the later date comes first in the expression

With no parameter entered within the () of dateDiff() the results are returned as days however entering a unit within the bracket will return the result in the selected unit

The units supported are:

  • 'd' -> days
  • 'w' -> weeks
  • 'm' -> months
  • 'y' -> years

Note: Parameters should be wrapped in ' quotation marks within the ()

Difference between two date properties

As well as entering fixed dates within the expression dateDiff() will also work with date properties

To calculate the age in whole years of an employee when they joined the company using the 2 existing properties; 'start date' and 'date of birth' with the 'y' parameter and add .floor()

node.startDate.dateDiff(node.dateOfBirth, 'y').floor()

image

Aggregated Difference between dates

When used with the Map() function dateDiff() will also work with Aggregators to provide the average, minimum or maximum difference between two dates for all employees

To calculate the age of the oldest employee to join the organisation

Use dateDiff() and the aggregator max

nodes().map(node => node.startDate.dateDiff(node.dateOfBirth, 'y')).max

image

results matching ""

    No results matching ""

    results matching ""

      No results matching ""