Add durationFromNow
This commit is contained in:
parent
78856ca62b
commit
06b11adb01
|
@ -51,6 +51,12 @@ const ADDED_HELPERS = {
|
||||||
description:
|
description:
|
||||||
"Gets the difference between two dates, in milliseconds. Pass a third parameter to adjust the unit measurement.",
|
"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.",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -121,7 +121,7 @@ export const date = (str: any, pattern: any, options: any) => {
|
||||||
return date.format(config.pattern)
|
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)
|
const config = initialConfig(str, pattern)
|
||||||
|
|
||||||
setLocale(config.str, config.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)
|
const result = dayjs(new Date(from)).diff(dayjs(new Date(to)), units)
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const durationFromNow = (from: string) => {
|
||||||
|
const diff = difference(from, new Date().toISOString(), "ms")
|
||||||
|
return duration(diff, "ms")
|
||||||
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
// @ts-ignore we don't have types for it
|
// @ts-ignore we don't have types for it
|
||||||
import helpers from "@budibase/handlebars-helpers"
|
import helpers from "@budibase/handlebars-helpers"
|
||||||
|
|
||||||
import { date, difference, duration } from "./date"
|
import { date, difference, duration, durationFromNow } from "./date"
|
||||||
import {
|
import {
|
||||||
HelperFunctionBuiltin,
|
HelperFunctionBuiltin,
|
||||||
EXTERNAL_FUNCTION_COLLECTIONS,
|
EXTERNAL_FUNCTION_COLLECTIONS,
|
||||||
|
@ -12,6 +12,7 @@ const ADDED_HELPERS = {
|
||||||
date,
|
date,
|
||||||
duration,
|
duration,
|
||||||
difference,
|
difference,
|
||||||
|
durationFromNow,
|
||||||
}
|
}
|
||||||
|
|
||||||
export const externalCollections = EXTERNAL_FUNCTION_COLLECTIONS
|
export const externalCollections = EXTERNAL_FUNCTION_COLLECTIONS
|
||||||
|
|
|
@ -1231,6 +1231,13 @@
|
||||||
],
|
],
|
||||||
"example": "{{ difference \"2025-09-30\" \"2025-06-17\" \"seconds\" }} -> 9072000",
|
"example": "{{ difference \"2025-09-30\" \"2025-06-17\" \"seconds\" }} -> 9072000",
|
||||||
"description": "<p>Gets the difference between two dates, in milliseconds. Pass a third parameter to adjust the unit measurement.</p>\n"
|
"description": "<p>Gets the difference between two dates, in milliseconds. Pass a third parameter to adjust the unit measurement.</p>\n"
|
||||||
|
},
|
||||||
|
"durationFromNow": {
|
||||||
|
"args": [
|
||||||
|
"time"
|
||||||
|
],
|
||||||
|
"example": "{{durationFromNow \"2025-09-30\"}} -> 7 months",
|
||||||
|
"description": "<p>Produce a humanized duration left/until given an amount of time and the type of time measurement.</p>\n"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue