Tutorial - Generate and format today's date with am expression
In this tutorial, you'll learn how to generate today's date and format it using JavaScript's date object inside a Cargo expression. Dates are used in various applications, for instance to log events or timestamp data entries into the CRM.
Before you begin
Make sure you have a basic understanding of Cargo expressions and how to reference nodes in your workflow.
Generate a date using a Javascript function
{{ new Date() }}
Convert date to ISO string
To format the date, first convert it to an ISO string.
This ensures a consistent and easy-to-manipulate format for later.
Convert date to ISO string
{{ new Date().toISOString() }}
Extract the date portion
ISO strings include both the date and the time, separated by a 'T' character.
Extract date portion
{{ new Date().toISOString().split('T')[0] }}
To isolate the date portion, split the string at 'T' and select the first element of the resulting array:
Execute the node and review the output
Click the play
button located in the right panel to process the
expression. After running the expression, check the OUT
tab that appears
for the result.
If there's a syntax error, a red banner will display at the top of the left panel, similar to the following message: "new Date(...).toISOString(...).split(...). is not a function".
Finish line
Once completed, the resulting string will contain today's date in the format
YYYY-MM-DD
, use this method to format a date as required by your workflow.