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

When working with dates and times in programmable decisions, it's important to choose an approach that balances functionality with performance.

Warning

Programmable decision performance impacted by moment.js library

Performance impact: Loading the moment.js and moment-timezone.js libraries can slow down programmable decision execution when parsing time zone data.

Recommendation: We strongly recommend using native date handling methods (Date and Intl.DateTimeFormat) for faster, more efficient programmable decisions.

Using native date handing (recommended)

Native JavaScript features like Date and Intl.DateTimeFormat are built into the browser, execute faster, and work well for most scenarios in programmable decisions.

Example: Get local time and time zone

This example shows how to return the current date and time in ISO format, along with the user's local time zone.

RequestResponse
function main() {
  // Create a new Date object
  var date = new Date();

  // Get local time in ISO format
  var localTime = date.toISOString();

  // Use Intl API to get the user's local time zone
  var timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;

  return {
    localTime: localTime,
    timeZone: timeZone
  };
}

main();

Sample output:

RequestResponse
{
  "localTime": "2025-07-23T10:15:30.000Z",
  "timeZone": "Europe/Berlin"
}

Example: Calculate difference in years between two dates

This example shows how to calculate the number of full years between two dates. It adjusts the result if the end date hasn't reached the same month and day as the start date.

RequestResponse
function main() {
  var fromDate = new Date('2010-05-20');
  var toDate = new Date('2019-05-20');

  // Calculate full year difference
  var years = toDate.getFullYear() - fromDate.getFullYear();

  // Adjust if the current date hasn't reached the month/day yet
  if (
    toDate.getMonth() < fromDate.getMonth() ||
    (toDate.getMonth() === fromDate.getMonth() && toDate.getDate() < fromDate.getDate())
  ) {
    years--;
  }

  return years;
}

main(); // Returns: 9

Using moment.js

The moment.js library enables you to parse, validate, manipulate, and display dates and time formats in JavaScript. Only use the moment.js library if your programmable decision requires advanced date parsing or formatting that cannot be handled natively. Otherwise, avoid loading the library to preserve performance.

Note

For optimal performance, refer to the Performance guidelines when using external libraries.

To use the moment.js library, include the following code snippet in the programmable decision.

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

}

Example: Parse a date and check if its within range

The following example shows 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;

Example: Format UTC dates to specific time zones

There is often a need to convert UTC dates to a specific timezone. The following example shows how to use the moment-timezone.js library to convert the UTC date to a specific time zone, while also taking into account daylight savings. Make sure to load both libraries in the correct order:

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-2026 Sitecore