Global Helpers
distanceOfTimeInWords()
Signature
Section titled “Signature”distanceOfTimeInWords() — returns string
Available in: controller, model, mapper, migrator, migration, tabledefinition
Category: Date Functions
Description
Section titled “Description”Pass in two dates to this method, and it will return a string describing the difference between them.
Parameters
Section titled “Parameters”| Name | Type | Required | Default | Description |
|---|---|---|---|---|
fromTime | date | yes | — | Date to compare from. |
toTime | date | yes | — | Date to compare to. |
includeSeconds | boolean | no | false | Whether or not to include the number of seconds in the returned string. |
Examples
Section titled “Examples”// 1. Compare two dates about a month apart
rightNow = Now();
aWhileAgo = DateAdd("d", -30, rightNow);
writeOutput(distanceOfTimeInWords(aWhileAgo, rightNow));
// -> "about 1 month"
// 2. Show a short gap with seconds included
justNow = Now();
fiveSecondsAgo = DateAdd("s", -5, justNow);
writeOutput(distanceOfTimeInWords(fiveSecondsAgo, justNow, includeSeconds=true));
// -> "less than 5 seconds"
// 3. Show various time ranges
now = Now();
writeOutput(distanceOfTimeInWords(DateAdd("n", -2, now), now));
// -> "2 minutes"
writeOutput(distanceOfTimeInWords(DateAdd("h", -3, now), now));
// -> "about 3 hours"
writeOutput(distanceOfTimeInWords(DateAdd("d", -5, now), now));
// -> "5 days"
writeOutput(distanceOfTimeInWords(DateAdd("yyyy", -2, now), now));
// -> "over 2 years"