The dateBetween()
function returns the amount of time between two dates, based on a specified unit of time.
dateBetween(date, date, string [from unit list])
date.dateBetween(date, string [from unit list])
Code language: JavaScript (javascript)
The function returns a number, and requires three arguments in the following order:
- Date 1 (must be a date data type)
- Date 2 (must be a date data type)
- A unit
Accepted units include:
- “years”
- “quarters”
- “months”
- “weeks”
- “days”
- “hours”
- “minutes”
- “seconds”
- “milliseconds”
dateBetween()
returns a positive number when the first date is later than the second date.
Good to know: You can use the abs function to ensure the output is always positive.
Example Formula
/* Assume now() == June 23, 2022 and Date == June 1, 2022 */
dateBetween(now(), prop("Date"), "days") /* Output: 22 */
/* Assume now() == June 23, 2022 and Date == June 30, 2022 */
now().dateBetween(prop("Date"), "days") /* Output: -6 */
/* Assume now() == June 23, 2022 and Date == December 25, 2022 */
dateBetween(now(),prop("Date"),"months") /* Output: -6 */
Code language: JavaScript (javascript)
Example Database
This example database uses the Birth Date property to determine the age of each person. Additional logic is included to deal with plurality (”year” vs “years”), and to express infant age in months.
View and Duplicate Database
“Age” Property Formula
dateBetween(now(), prop("Birth Date"), "years")
Code language: JavaScript (javascript)
“Age (Pretty)” Property Formula
lets(
ageDays,
dateBetween(
now(),
prop("Birth Date"),
"days"
),
ageMonths,
dateBetween(
now(),
prop("Birth Date"),
"months"
),
ageYears,
dateBetween(
now(),
prop("Birth Date"),
"years"
),
prop("Name") + " is " +
ifs(
!ageYears and !ageMonths,
ageDays + " day" + ifs(ageDays != 1, "s") + " old",
!ageYears,
ageMonths + " month" + ifs(ageMonths != 1, "s") + " old",
ageYears + " year"+ ifs(ageYears != 1, "s") + " old"
) + "."
)
Code language: JavaScript (javascript)
Here, we create a “pretty” sentence that states the person’s name along with their age.
A nested if-statement is used to determine whether the age meets certain criteria. Depending on the result, the formula will return different ending strings:
- If the person is less than 1 year old, the ending output will be “months old” or “month old” depending on the number of months.
- If the person is exactly 1 year old, the ending output will be “year old.”
- Otherwise, the ending output will be “years old”.