Including dates in programmable decisions
To avoid errors when including dates and times in programmable decisions, use the moment.js
library. The moment.js library enables you to parse, validate, manipulate, and display dates and time formats in JavaScript.
Decision Model Performance impacted by moment.js library
Only load the moment.js library in a programmable decision, if it includes date and time formats. This helps ensure maximum decision model performance. See Decision Model Performance Guidelines for more information.
To use the moment.js library, include the following code snippet in the programmable decision.
load("classpath:moment.js");
The following examples illustrate how to parse a date and determine whether a flight's departure date is between 1 and 365 days out.
load("classpath:moment.js");
// to parse a date, eg. 2016-09-27T17:50:00.000Z
var departureDateTime = moment(flightSegments[0].departureDateTime);
// expanded example for finding if a flight's departure is between 1 and 365 days out
var departureDateTime = moment(flightSegments[0].departureDateTime);
var departureWindowStart = moment().add(1, 'days').startOf('day');
var departureWindowEnd = moment().add(365, 'days').startOf('day');
if (departureDateTime.isBetween(departureWindowStart, departureWindowEnd)) {
return true;
}
Format UTC dates to specific timezones
There is often a need to convert UTC dates to a specific timezone. For example, you might want to display the date and time of an event for a specific timezone. For this reason, you can use the moment-timezone.js
library to convert the UTC date to a specific timezone, while also taking into account daylight savings.
To use the moment-timezone.js
library, include the following code snippet in the programmable decision. Ensure that you load the moment.js
library first.
load("classpath:moment.js");
load("classpath:moment-timezone.js");