The Date/getDate extension calculates a new date from inside a Virtuoso journey by applying date and time adjustments to a base date.
This is useful when your test needs to generate dynamic dates, add days or months, calculate the start or end of a month, or return the result in a specific date format.
Parameters:
-
dateObjectrequired, a valid JSON object containing the date or time adjustments to apply, for example{"days":5,"weeks":0,"months":0,"quarters":0,"years":0,"hours":0,"minutes":0,"seconds":0}; -
dateInputoptional, the base date to calculate from. If this is not provided, the extension uses the current date in the detected timezone; -
formatoptional, the input and output date format, for exampleYYYY-MM-DD,DD/MM/YYYY, orYYYY/MM/DD HH:mm:ss; -
startOrLastDayInAMonthoptional, usestartDayOfMonthto return the first day of the month orlastDayOfMonthto return the last day of the month.
Note: The dateObject value must be valid JSON. Supported keys include days, weeks, months, quarters, years, hours, minutes, and seconds.
NLP usage
Use the extension in a journey by calling Date/getDate with the execute command. Pass each value to the matching extension input using as inputName.
Note: If format is provided, the extension uses that format for both parsing the input date and formatting the output date. If no format is provided, it attempts to parse using the supported formats listed in the source.
To add five days to a date:
execute "Date/getDate" using '{"days":5,"weeks":0,"months":0,"quarters":0,"years":0,"hours":0,"minutes":0,"seconds":0}' as dateObject, "2024-01-10" as dateInput, "YYYY-MM-DD" as format, "" as startOrLastDayInAMonth returning $dateResultTo return the first day of the month for a date:
execute "Date/getDate" using '{"days":0,"weeks":0,"months":0,"quarters":0,"years":0,"hours":0,"minutes":0,"seconds":0}' as dateObject, "2024-01-10" as dateInput, "YYYY-MM-DD" as format, "startDayOfMonth" as startOrLastDayInAMonth returning $dateResultTo return the last day of the month for a date:
execute "Date/getDate" using '{"days":0,"weeks":0,"months":0,"quarters":0,"years":0,"hours":0,"minutes":0,"seconds":0}' as dateObject, "2024-01-10" as dateInput, "YYYY-MM-DD" as format, "lastDayOfMonth" as startOrLastDayInAMonth returning $dateResultYou can also use Virtuoso variables to make the same step reusable across different dates, formats, or adjustment values:
execute "Date/getDate" using "$dateObject" as dateObject, "$dateInput" as dateInput, "$dateFormat" as format, "$monthBoundary" as startOrLastDayInAMonth returning $dateResultExample setup using variables before calling the extension:
store value '{"days":5,"weeks":0,"months":0,"quarters":0,"years":0,"hours":0,"minutes":0,"seconds":0}' in $dateObject
store value "2024-01-10" in $dateInput
store value "YYYY-MM-DD" in $dateFormat
store value "" in $monthBoundary
execute "Date/getDate" using "$dateObject" as dateObject, "$dateInput" as dateInput, "$dateFormat" as format, "$monthBoundary" as startOrLastDayInAMonth returning $dateResultFor example, adding five days to 2024-01-10 returns:
2024-01-15This extension requires the following resources:
https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.jshttps://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.43/moment-timezone-with-data.min.js
The extension should be configured as:
- Run asynchronously: No
- Scope: Global
Limitation: The custom timezone input is not enabled in the current source. The extension uses moment.tz.guess() when no timezone is provided in the code.
Add the extension to your Virtuoso instance
Select the domain that matches your Virtuoso account.
View source
Last updated: 22/05/2026
Resources:
https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.jshttps://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.43/moment-timezone-with-data.min.js
/*const objectEntry = '{"days": 5, "weeks": 0, "months": 0, "quarters": 0, "years": 0, "hours": 0, "minutes": 0, "seconds": 0}';
const dateInput = '2024-01-10';
const format = 'YYYY-MM-DD';
*/
let inputValues;
try {
inputValues = JSON.parse(dateObject);
} catch (error) {
throw new Error(`"Invalid input to dateObject. Please provide a valid JSON object.\nExample: {"days": 5, "weeks": 0, "months": 0, "quarters": 0, "years": 0, "hours": 0, "minutes": 0, "seconds": 0}"`);
}
const inputDate = dateInput;
const userProvidedFormat = format;
//CustomTimeZone Param is not enabled for use, it was set to default in the code and which can be enabled based on the needs
const customTimezone = '';
const possibleFormats = [
"YYYY-MM-DD",
"DD/MM/YYYY",
"MM/DD/YYYY",
"DD.MM.YYYY",
"YYYY/MM/DD",
"DD-MM-YYYY",
"MM-DD-YYYY",
"DD MMM YYYY",
"MMM DD, YYYY",
"MMMM DD, YYYY",
"YYYY MMM DD",
"YYYY-DD-MM",
"YYYY/MM/DD HH:mm:ss",
"DD.MM.YYYY HH:mm:ss",
"DD-MM-YYYY HH:mm:ss",
"MM/DD/YYYY HH:mm:ss",
// Add more formats as needed
];
const userInput = startOrLastDayInAMonth; // Set to 'startDayOfMonth' or 'lastDayOfMonth' if needed
const dynamicInputValues = {
...inputValues,
...(userInput && { [userInput]: true }),
};
let result = calculateDate(inputDate, dynamicInputValues, userProvidedFormat, possibleFormats, customTimezone);
console.log(result);
return result;
function calculateDate(inputDate, inputValues, format, possibleFormats, timezone) {
timezone = timezone || moment.tz.guess();
let baseMoment;
if (format) {
baseMoment = inputDate
? moment.tz(inputDate, format, timezone)
: moment.tz(timezone).startOf('day');
} else {
for (const possibleFormat of possibleFormats) {
baseMoment = inputDate
? moment.tz(inputDate, possibleFormat, timezone)
: moment.tz(timezone).startOf('day');
if (baseMoment.isValid()) {
break;
}
}
}
if (!baseMoment.isValid()) {
throw new Error("Invalid input date");
}
let result = baseMoment.clone();
for (const [input, value] of Object.entries(inputValues)) {
switch (input) {
case 'days':
case 'weeks':
case 'months':
case 'quarters':
case 'years':
case 'hours':
case 'minutes':
case 'seconds':
result = result.add(value, input);
break;
case 'startDayOfMonth':
result = result.startOf('month');
break;
case 'lastDayOfMonth':
result = result.endOf('month');
break;
default:
throw new Error("Invalid input");
}
}
return result.format(format || possibleFormats[0]);
}
Comments
0 comments
Please sign in to leave a comment.