The calculateDate extension calculates a date by adding or subtracting a time value from a supplied date string. It can return the result in a custom Moment.js date format or, when no output format is supplied, in the default YYYY-MM-DD format.
This is useful when a journey needs dynamic dates, such as checking tomorrow's booking date, calculating an expiry date, validating a renewal date, or preparing a date value before entering it into an application field.
Parameters:
-
daterequired, the starting date string to calculate from. ISO 8601 input is recommended; other formats depend on Moment.js parsing behavior and the supplieddateFormatvalue; -
operationrequired, the string operation to perform. Useaddorsubtract; -
valuerequired, the numeric value to add or subtract. The source converts this input usingNumber(value), so pass a number or numeric string; -
unitrequired, the Moment.js duration unit to apply, such asdays,weeks,months, oryears; -
dateFormatoptional, the Moment.js output format string. If not provided, the extension formats the result asYYYY-MM-DD.
Note: Use ISO 8601 dates where possible, for example 2025-04-29. The current source returns the result using the supplied dateFormat, but its parsing line uses moment(date) when dateFormat is supplied and moment(date, dateFormat) when it is not supplied. This means custom-format input may still depend on Moment.js fallback parsing unless the source is corrected.
How to apply this to your journey
Use the extension in a journey by calling calculateDate with the execute command. Pass each value to the matching extension input using as inputName.
Note: The extension returns a string. Store the result in a variable and then use a normal assertion, comparison, or write step. Invalid operations throw an error and fail the extension step.
execute "calculateDate" using "2025-04-29" as date using "add" as operation using "1" as value using "days" as unit returning $newDate
assert $newDate equals "2025-04-30"execute "calculateDate" using "2025-04-29" as date using "subtract" as operation using "2" as value using "weeks" as unit using "YYYY-MM-DD" as dateFormat returning $newDate
assert $newDate equals "2025-04-15"You can also pass values from journey variables. This is useful when the base date or date adjustment is produced by another step.
execute "calculateDate" using "$baseDate" as date using "$operation" as operation using "$amount" as value using "$unit" as unit using "$outputFormat" as dateFormat returning $calculatedDatestore value "2025-04-29" in $baseDate
store value "add" in $operation
store value "1" in $amount
store value "months" in $unit
store value "YYYY-MM-DD" in $outputFormat
execute "calculateDate" using "$baseDate" as date using "$operation" as operation using "$amount" as value using "$unit" as unit using "$outputFormat" as dateFormat returning $calculatedDateExample output when adding one month to 2025-04-29 with YYYY-MM-DD formatting:
2025-05-29This extension requires the following resources:
https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.2/moment.min.jshttps://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.31/moment-timezone-with-data.min.js
The extension should be configured as:
- Run asynchronously: No
- Scope: Global
Limitation: This extension depends on Moment.js and the browser execution context available to the Virtuoso journey step. It performs a simple add/subtract operation and returns a formatted string; it does not validate business calendars, holidays, working days, timezone rules, daylight-saving expectations, or locale-specific date policy beyond what Moment.js applies. The source converts value with Number(value), so non-numeric values can produce invalid results. The source also appears to reverse the intended parsing condition: when dateFormat is provided it calls moment(date), and when it is not provided it calls moment(date, dateFormat). Because of this, non-ISO or ambiguous dates such as 12/25/1995 may depend on Moment.js fallback parsing instead of strict format-based parsing unless the source is corrected. Moment.js is a legacy project in maintenance mode, so date parsing and formatting behavior should be validated for your supported formats before relying on it in production journeys. The included Moment Timezone resource is loaded, but this source does not call moment.tz() or accept a timezone input, so timezone-sensitive journeys should not assume a specific target timezone from this extension alone. Cross-browser note: This extension is mostly standard JavaScript plus external browser libraries, but cross-browser or real-device executions can still vary because CDN loading, browser date parsing fallback, locale defaults, and execution environment can differ across Chromium, Firefox, Safari, Edge, iOS, Android, and remote-grid configurations. Validate it in each browser/device configuration used by your plan, especially when using non-ISO input dates, localized formats, or timezone-sensitive assertions.
Add the extension to your Virtuoso instance
Select the domain that matches your Virtuoso account.
View source
Last updated: 29/04/2025
Resources:
https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.2/moment.min.jshttps://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.31/moment-timezone-with-data.min.js
// Last updated: 29/04/2025, 11:25:53 UTC
// Resources:
// https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.2/moment.min.js
// https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.31/moment-timezone-with-data.min.js
function manipulateDate(date, operation, value, unit) {
const momentDate = dateFormat ? moment(date) : moment(date, dateFormat);
if (operation === "add") {
return momentDate.add(value, unit).format(dateFormat ? dateFormat : "YYYY-MM-DD");
} else if (operation === "subtract") {
return momentDate.subtract(value, unit).format(dateFormat ? dateFormat : "YYYY-MM-DD");
} else {
throw new Error("Invalid operation. Use 'add' or 'subtract'.");
}
}
/**
* @param date The initial date string, more info visit https://momentjs.com/docs/#/parsing/
* @param operation The operation to perform (e.g., "add", "subtract")
* @param unit The unit of time (e.g., "days", "months", "years")
* @param value The value to add or subtract
* @param dateFormat optional pramater to specify date format defaults to 'YYYY-MM-DD'
* @returns The manipulated date as a string
* @example Execute manipulateDate("2023-01-01", "add", "5", "days") returning $result
*/
return manipulateDate(date, operation, Number(value), unit);
Comments
0 comments
Please sign in to leave a comment.