---
title: "プログラム可能な決定における日付も含む"
slug: "including-dates-in-programmable-decisions"
abstract: "プログラム可能な意思決定に日付と時間を含める方法の概要(Sitecore Personalize)。"
---
# プログラム可能な決定における日付も含む

プログラム可能な意思決定において日付や時間を使う際には、機能性とパフォーマンスのバランスを取るアプローチを選ぶことが重要です。

> !警告moment.jsライブラリによるプログラム可能な意思決定パフォーマンスへの影響
> 
> **パフォーマンスへの影響**: moment.jsおよびmoment-timezone.jsライブラリの読み込みは、タイムゾーンデータを解析する際のプログラム可能な意思決定実行を遅くする可能性があります。
> 
> **推奨**:より迅速かつ効率的なプログラム可能な意思決定のために、ネイティブの日付処理手法(DateとIntl.DateTimeFormat)を使用することを強く推奨します。

## ネイティブの日付処理法の使用(推奨)

DateやIntl.DateTimeFormatのようなネイティブJavaScript機能はブラウザに組み込まれており、より高速に実行され、プログラム可能な意思決定のほとんどのシナリオでうまく機能します。

### 例:現地の時間とタイムゾーンを取得します

この例では、現在の日付と時刻をISO形式で返す方法、そしてユーザーの現地タイムゾーンを示します。

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();

サンプル出力:

{
 "localTime": "2025-07-23T10:\`15:30\`.000Z",
 "timeZone": "Europe/Berlin"
}

### 例:2つの日付間の年差を計算する

この例は、2つの日付間の完全な年数の計算方法を示しています。終了日が開始日と同じ月・日に届いていない場合、結果を調整します。

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

## moment.jsの使い方

[moment.js](https://momentjs.com/)ライブラリは、日付や時刻のフォーマットをJavaScriptで解析、検証、操作、表示を可能にします。ネイティブで扱えない高度な日付解析やフォーマットが必要な場合のみ、プログラム可能な決定でmoment.jsライブラリを使ってください。それ以外の場合は、パフォーマンスを保つためにライブラリのロードを避けてください。

> !注最適なパフォーマンスのためには、外部ライブラリを使用する際は [パフォーマンスガイドライン](https://doc.sitecore.com/personalize/ja/users/sitecore-personalize/introduction-to-decisioning-in-sitecore-personalize/managing-programmable-decisions/decision-model-performance-guidelines.html "Decision model performance guidelines") を参照してください。

moment.jsライブラリを使用するには、プログラム可能な判定に以下のコードスニペットを含めてください。

load("classpath:moment.js");

}

### 例:日付を解析して範囲内かどうか確認します

以下の例は、日付を解析し、出発日が1日前から365日前かの間かを判断する方法を示しています。

load("classpath:moment.js");
// to parse a date, eg. 2016-09-27T17:\`50:00\`.000Z

var departureDateTime = moment(flightSegments0.departureDateTime);

// expanded example for finding if a flight's departure is between 1 and 365 days out
var departureDateTime = moment(flightSegments0.departureDateTime);
var departureWindowStart = moment().add(1, 'days').startOf('day');
var departureWindowEnd = moment().add(365, 'days').startOf('day');
if (departureDateTime.isBetween(departureWindowStart, departureWindowEnd)) {
 return true;

### 例:UTC日付を特定のタイムゾーンにフォーマットする

UTCの日付を特定のタイムゾーンに変換する必要がよくあります。以下の例は、moment-timezone.jsライブラリを使ってUTC日付を特定のタイムゾーンに変換する方法と、サマータイムを考慮した方法を示しています。両方のライブラリを正しい順序で読み込むようにしてください:

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