From 06b11adb0192c77cb2f56476fb715e89a2249fbe Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Thu, 6 Mar 2025 11:28:49 +0100 Subject: [PATCH] Add durationFromNow --- packages/string-templates/scripts/gen-collection-info.ts | 6 ++++++ packages/string-templates/src/helpers/date.ts | 7 ++++++- packages/string-templates/src/helpers/external.ts | 3 ++- packages/string-templates/src/manifest.json | 7 +++++++ 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/packages/string-templates/scripts/gen-collection-info.ts b/packages/string-templates/scripts/gen-collection-info.ts index 406c2959a5..e0904f3953 100644 --- a/packages/string-templates/scripts/gen-collection-info.ts +++ b/packages/string-templates/scripts/gen-collection-info.ts @@ -51,6 +51,12 @@ const ADDED_HELPERS = { description: "Gets the difference between two dates, in milliseconds. Pass a third parameter to adjust the unit measurement.", }, + durationFromNow: { + args: ["time"], + example: '{{durationFromNow "2025-09-30"}} -> 7 months', + description: + "Produce a humanized duration left/until given an amount of time and the type of time measurement.", + }, }, } diff --git a/packages/string-templates/src/helpers/date.ts b/packages/string-templates/src/helpers/date.ts index 26460216ea..4aae9ae4a4 100644 --- a/packages/string-templates/src/helpers/date.ts +++ b/packages/string-templates/src/helpers/date.ts @@ -121,7 +121,7 @@ export const date = (str: any, pattern: any, options: any) => { return date.format(config.pattern) } -export const duration = (str: any, pattern: any, format: any) => { +export const duration = (str: any, pattern: any, format?: any) => { const config = initialConfig(str, pattern) setLocale(config.str, config.pattern) @@ -138,3 +138,8 @@ export const difference = (from: string, to: string, units: UnitType) => { const result = dayjs(new Date(from)).diff(dayjs(new Date(to)), units) return result } + +export const durationFromNow = (from: string) => { + const diff = difference(from, new Date().toISOString(), "ms") + return duration(diff, "ms") +} diff --git a/packages/string-templates/src/helpers/external.ts b/packages/string-templates/src/helpers/external.ts index de3f70f573..ddc3766da1 100644 --- a/packages/string-templates/src/helpers/external.ts +++ b/packages/string-templates/src/helpers/external.ts @@ -1,7 +1,7 @@ // @ts-ignore we don't have types for it import helpers from "@budibase/handlebars-helpers" -import { date, difference, duration } from "./date" +import { date, difference, duration, durationFromNow } from "./date" import { HelperFunctionBuiltin, EXTERNAL_FUNCTION_COLLECTIONS, @@ -12,6 +12,7 @@ const ADDED_HELPERS = { date, duration, difference, + durationFromNow, } export const externalCollections = EXTERNAL_FUNCTION_COLLECTIONS diff --git a/packages/string-templates/src/manifest.json b/packages/string-templates/src/manifest.json index 7b6ac7aca0..1ac9b30e08 100644 --- a/packages/string-templates/src/manifest.json +++ b/packages/string-templates/src/manifest.json @@ -1231,6 +1231,13 @@ ], "example": "{{ difference \"2025-09-30\" \"2025-06-17\" \"seconds\" }} -> 9072000", "description": "

Gets the difference between two dates, in milliseconds. Pass a third parameter to adjust the unit measurement.

\n" + }, + "durationFromNow": { + "args": [ + "time" + ], + "example": "{{durationFromNow \"2025-09-30\"}} -> 7 months", + "description": "

Produce a humanized duration left/until given an amount of time and the type of time measurement.

\n" } } } \ No newline at end of file