Unify Logo Footer.svg
Unify Applications
Logo
Built-in Utilities

Built-in Utilities

Logo

7 mins READ

Built-in Utilities

Every expression can use a small set of helpers under the utils namespace — no setup required, available in any binding on any page. They cover the everyday jobs of turning raw data into something a user can read: dates, durations, numbers, JSON, and file downloads. Each helper degrades quietly instead of throwing, so a formatting mistake never crashes your page.

Helpers at a Glance

HelperWhat it doesQuick example
utils.formatDateFormat a date with a custom date-fns pattern{{ utils.formatDate(order.createdAt, 'dd MMM yyyy') }}15 Jan 2024
utils.formatDateByFormatTypeFormat a date with a named preset, including relative forms like "2 hours ago"{{ utils.formatDateByFormatType(order.createdAt, 'Mdy') }}January 05, 2024
utils.formatTimeTurn a millisecond duration into readable units like 1d 1h 1m{{ utils.formatTime(90061000, 'seconds') }}1d 1h 1m 1s
utils.formatNumberFormat a number — locale grouping, compact notation, units{{ utils.formatNumber({ value: 1234567, notation: 'compact' }) }}1.2M
utils.parseJsonParse a JSON string safely — never throws{{ utils.parseJson(webhook.body).total }}250
utils.stringifyJsonSerialize a value to JSON safely — never throws{{ utils.stringifyJson(filters.value) }}{"status":"open"}
utils.downloadFileDownload a file in the current browser tab (side effect — use in actions, not rendered bindings){{ utils.downloadFile(invoice.fileUrl, 'invoice.pdf') }}
utils.dateTime.values.todayDateToday's date as dd-MM-yyyy (snapshot, not a live clock){{ utils.dateTime.values.todayDate }}06-07-2026
utils.dateTime.values.startOfTodayTimestampLocal midnight timestamp — use for "today only" filters{{ order.createdAt >= utils.dateTime.values.startOfTodayTimestamp }}
utils.getAuthTokenMobile apps only. Returns the signed-in session's auth token, or empty string.{{ utils.getAuthToken() }}

utils.formatDate

Formats a date or timestamp using a date-fns pattern (https://date-fns.org/docs/format).

utils.formatDate(date, formatStr)

ParameterTypeRequiredDescription
dateDate | string | numberRequired (required)A Date object, ISO string, or numeric timestamp. A numeric string (e.g. '1704067200000') is treated as an epoch timestamp — see gotchas.
formatStrstringRequired (required)Any date-fns pattern. Common tokens: dd day, MMM short month, yyyy year, H 24-hour, h 12-hour, mm minutes, a AM/PM.

Examples

{{ utils.formatDate(order.createdAt, 'dd MMM yyyy') }} // → 15 Jan 2024 {{ utils.formatDate(order.createdAt, 'MMM dd, yyyy h:mm a') }} // → Jan 15, 2024 10:30 AM {{ utils.formatDate('not-a-date', 'dd MMM yyyy') }} // → not-a-date (echoes invalid input)

utils.formatDateByFormatType

Formats a date using a named preset instead of a raw pattern — including relative forms like "2 hours ago".

utils.formatDateByFormatType(date, formatType, options)

ParameterTypeRequiredDefaultDescription
dateDate | string | numberRequired (required)The date to format.
formatTypestringOptional (optional)mdyhmA preset name from the table below, or any raw date-fns pattern. Unknown strings are treated as raw patterns.
optionsobjectOptional (optional)Only used by relative-time presets.

Date Format Presets

PresetPatternOutput (example: Jan 05, 2024, 09:07:03)
dmyhmsdd MMM yyyy, H:mm:ss05 Jan 2024, 9:07:03
dmyhmdd MMM yyyy, H:mm05 Jan 2024, 9:07
mdyhm (default)MM/dd/yyyy H:mm01/05/2024 9:07
MdyMMMM dd, yyyyJanuary 05, 2024
dMyd MMM yyyy5 Jan 2024
yyyyMMddyyyy-MM-dd2024-01-05
ddMMyyyydd-MM-yyyy05-01-2024
hhmmHH:mm09:07
hmah:mm a9:07 AM
isoyyyy-MM-dd'T'HH:mm:ssxxx2024-01-05T09:07:03+05:30
relative-time-agoRelative with "ago"2 hours ago (requires timestamp input)
relative-time-to-nowRelative with "about"about 2 hours

utils.formatTime

Turns a duration in milliseconds into readable units. This is elapsed time, not a clock time.

utils.formatTime(ms, precision, notation, maxSignificantUnits)

ParameterTypeDefaultDescription
msnumberDuration in milliseconds.
precisionstring'milliseconds'Smallest unit to show: 'years', 'months', 'days', 'hours', 'minutes', 'seconds', 'milliseconds'.
notationstring'compact''compact'1d 1h; 'comfortable'1 day 1 hour.
maxSignificantUnitsnumber7Show at most this many units (clamped 1–6).

Examples

{{ utils.formatTime(90061000, 'seconds') }} // → 1d 1h 1m 1s {{ utils.formatTime(90061000, 'seconds', 'comfortable') }} // → 1 day 1 hour 1 minute 1 second {{ utils.formatTime(90061000, 'seconds', 'compact', 2) }} // → 1d 1h

utils.formatNumber

Formats a number with locale grouping, compact notation, or units. Takes a single options object — not a plain number.

utils.formatNumber({ value, locale, notation, compactDisplay, maximumFractionDigits, padDecimalPlaces, unit, unitDisplay })

PropertyTypeRequiredDefaultDescription
valuenumberRequired (required)The number to format.
localestringOptional (optional)Runtime localee.g. 'en-US', 'de-DE'
notationstringOptional (optional)'standard''standard', 'compact', 'scientific', 'engineering', or 'text' (disables grouping).
compactDisplaystringOptional (optional)With compact notation: 'short' (1.2M) or 'long' (1.2 million).
maximumFractionDigitsnumberOptional (optional)Maximum digits after the decimal.
padDecimalPlacesbooleanOptional (optional)Pad decimals with zeros to maximumFractionDigits. Has no effect without maximumFractionDigits.
unitstringOptional (optional)e.g. 'percent', 'kilometer-per-hour', 'megabyte'.
unitDisplaystringOptional (optional)'short', 'narrow', or 'long'.

Examples

{{ utils.formatNumber({ value: 1234567.891, maximumFractionDigits: 2 }) }} // → 1,234,567.89 {{ utils.formatNumber({ value: 1234567, notation: 'compact', compactDisplay: 'short', maximumFractionDigits: 1 }) }} // → 1.2M {{ utils.formatNumber({ value: 5, maximumFractionDigits: 2, padDecimalPlaces: true }) }} // → 5.00 {{ utils.formatNumber({ value: 20, unit: 'percent' }) }} // → 20% {{ utils.formatNumber({ value: 20, unit: 'kilometer-per-hour', unitDisplay: 'long' }) }} // → 20 kilometers per hour

Warning: utils.formatNumber does not support currency formatting. To show monetary values, format the number and add the currency symbol yourself in your expression: {{ '$' + utils.formatNumber({ value: total, maximumFractionDigits: 2 }) }}

utils.parseJson

Parses a JSON string safely. Invalid JSON never throws — you get the fallback value instead.

utils.parseJson(str, fallback, retriever)

ParameterTypeDefaultDescription
strstringThe JSON text to parse.
fallbackany{}Returned on a parse error and when the parsed value is null.
retrieverfunctionA JSON reviver — advanced, usually omitted.

Examples

{{ utils.parseJson(webhook.body).total }} // → 250 {{ utils.parseJson('oops', []) }} // → [] (fallback) {{ utils.parseJson('null') }} // → {} (null result → fallback)

utils.stringifyJson

Serializes a value to a JSON string. On failure (e.g. circular reference) returns the fallback instead of throwing.

utils.stringifyJson(obj, fallback, space, replacer)

ParameterTypeDefaultDescription
objanyThe value to serialize.
fallbackstringReturned on failure. If absent, failure returns null.
spacenumber0Indentation for pretty-printing.
replacerfunctionJSON replacer function — advanced, usually omitted.

utils.downloadFile

Starts a file download in the current browser tab. This is a side effect — put it in an action (button's On Click), not in a text/label binding that recomputes on every render.

utils.downloadFile(fileOrUrl, filename)

ParameterTypeDescription
fileOrUrlstring | BlobA file URL/path, or in-memory file content. Platform file URLs are resolved automatically.
filenamestringThe name to save the file as.

Usage — in a button's On Click action

// Use in a Run Script action on the button, NOT in a text binding {{ utils.downloadFile(invoice_query.data.fileUrl, 'invoice-2024.pdf') }}

Gotchas

GotchaDetails
Helpers degrade silentlyNone of these helpers throw on bad input — they return a safe fallback. A formatting mistake shows as unformatted output, not an error screen.
Numeric string = timestamp in formatDateA string of digits (e.g. '1704067200000') is treated as an epoch timestamp by formatDate, not as text.
formatNumber takes an object, not a numberWrite {{ utils.formatNumber({ value: total }) }} — not {{ utils.formatNumber(total) }}. Passing a plain number returns the number unchanged.
downloadFile is a side effectA binding re-evaluates on every render. Placing utils.downloadFile in a text/label binding downloads on every re-render. Place it in button actions only.
formatTime = elapsed time, not clock timeTakes a duration in milliseconds, not a time of day. If precision is coarser than the whole span (e.g. hours on a 90-second duration), the result is an empty string — not "0h".
relative-time-ago needs a timestampThe relative-time-ago preset reads its input as a number. An ISO date string produces an invalid result — use relative-time-to-now for ISO strings.
dateTime values are snapshotstodayDate and startOfTodayTimestamp are computed when the page context is built — they reflect the time of last rebuild, not a live clock.
getAuthToken is mobile-onlyutils.getAuthToken() does not exist on web apps — it is only available in mobile apps.