Articles in this section

Access the dayjs JavaScript library in scripts

The integrator.io JavaScript runtime natively supports dayjs, a library for parsing, validating, manipulating, and displaying dates and times. Use it in a script to reformat dates between source and destination applications, compare a date against the current time, or derive values such as a fiscal quarter.

This article covers the supported version, the import statement, plugin usage, and a worked example.

Note: The JavaScript runtime module doesn't support dayjs timezone.

Supported dayjs version

integrator.io supports dayjs 1.11.0, released under the MIT license. It's widely used for parsing, validating, manipulating, and displaying date and time values, and if you've worked with Moment.js, dayjs is similar. Features from other versions aren't available.

Import dayjs

Include this statement at the start of your script. integrator.io imports the library when the script runs.

import dayjs from 'dayjs'

Some dayjs functions live in plugins, which you import and register separately. Quarter calculations need the quarterOfYear plugin:

import dayjs from 'dayjs'
import quarterOfYear from 'dayjs/plugin/quarterOfYear'

dayjs.extend(quarterOfYear)

Example: derive billing status from a date

This example uses the preSavePage function stub, which runs on exports, listeners, and lookups after data retrieval. It reformats a bill date, checks whether that date is in the past or the future, and calculates its quarter — writing each result onto the record so you can map it to a destination field.

Note: You can create a script using a function stub in integrator.io. Select the stub that matches where in the flow you want the logic to run, then add your dayjs calls inside it.

/*Script using the preSavePage function stub that includes dayjs functions*/

import dayjs from 'dayjs'
import quarterOfYear from 'dayjs/plugin/quarterOfYear' // Import plugin

function preSavePage (options) {
  dayjs.extend(quarterOfYear)
  var BillDate = options.data[0].enddate;
  /* Convert the date from 'DD-MM-YYY' to 'YYYY-MM-DD' format */
  var BillingDate = dayjs(BillDate).format('YYYY-MM-DD')
  /* Return Boolean value if the bill date is still pending */
  var PendingBillingDate = dayjs(BillDate).isBefore()
  /* Return Boolean value if the bill date is missed */
  var MissedBillingDate = dayjs(BillDate).isAfter()
  /* Return quarterly term date */
  var Quarterlyterm = dayjs(BillDate).quarter()

  options.data[0].BillingDate = BillingDate
  options.data[0].BillDate = BillDate
  options.data[0].PendingBillingDate = PendingBillingDate
  options.data[0].MissedBillingDate =  MissedBillingDate
  options.data[0].Quarterlyterm =  Quarterlyterm

  return {
    data: options.data,
    errors: options.errors,
    newErrorsAndRetryData: []
  }
}

Called with no argument, isBefore() and isAfter() compare the date against the current date and time. The example reads options.data[0] to keep the code short; in a flow that processes batches, iterate over options.data so every record in the page is handled.

Common dayjs operations

Task Call
Parse a date value dayjs(value)
Reformat a date dayjs(value).format('YYYY-MM-DD')
Check whether a date is in the past dayjs(value).isBefore()
Check whether a date is in the future dayjs(value).isAfter()
Get the quarter of the year dayjs(value).quarter() — requires the quarterOfYear plugin

For the full library API, see the dayjs documentation.