Skip to main content
Users
CloudPortalLogin
  • Powered byPowered by
Introduction to Sitecore Personalize
Managing accounts and system settings
Identifying guests
Introduction to experiments
Introduction to experiences
Decisioning
View dashboards
Developer Center
Connecting to an external system
Using client-side JavaScript
Using server-side JavaScript
AI in Personalize
Glossary
  • Sitecore Personalize
  • Introduction to decisioning in Sitecore Personalize
  • Managing programmable decisions
  • Including dates in programmable decisions

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.

Warning

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.

RequestResponse
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.

RequestResponse
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.

RequestResponse
load("classpath:moment.js");
load("classpath:moment-timezone.js");

Do you have some feedback for us?

If you have suggestions for improving this article,

Privacy policySitecore Trust CenterCopyright © 1999-2025 Sitecore