Creating date-related values using JSONata

Many nodes in OL Connect Automate support JSONata expressions, such as the Set Properties, Change, and Inject nodes. The JSONata expression language is used to format, rearrange, and combine data. This tutorial demonstrates several date-related options using the $moment function.

The $moment function in JSONata is a powerful tool for working with dates and times. Inspired by the popular Moment.js library, it allows for easy manipulation and formatting of date and time values. Here’s how to use $moment to create date-related information:

Custom Date format

To format the current date and time in a custom format, you can use the $moment().format() function. For example:

$moment().format("YYYYMMDD_hhmmss")

An explanation on the various tokens can found in the displaying section of the moment.js documentation.

Example result

20240708_043210

Removing the time related tokens lets you to create a simple custom date string (change the order of the tokens to match the prefered notation):

$moment().format("YYYYMMDD")

Here’s how to use math (substract()) to retrieve yesterday’s date:

$moment().subtract(1, 'days').format("YYYYMMDD")

Current Week number

To find out the current week of the year, use:

$moment().week()

Current Month number

To get the current month number (0-11), use:

$moment().month()

This returns the month of the year where January is 0 and December is 11.

Year and Month

To combine the current year and month in a custom format, you can use:

$moment().year() & "_" & $moment().month()

Explanation:

  • year(): Returns the 4-digit year.
  • month(): Returns the month number (0-11).
  • & "_": Concatenates the year, an underscore, and the month.

Example result

2024_6

Summary

By using these expressions, you can easily manipulate and format dates within JSONata to suit your needs. For example, they can be used to set properties for content sets created for daily, weekly, or monthly productions.