プログラム可能な決定における日付も含む
このページの翻訳はAIによって自動的に行われました。可能な限り正確な翻訳を心掛けていますが、原文と異なる表現や解釈が含まれる場合があります。正確で公式な情報については、必ず英語の原文をご参照ください。
プログラム可能な意思決定において日付や時間を使う際には、機能性とパフォーマンスのバランスを取るアプローチを選ぶことが重要です。
!警告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
`.000Z", "timeZone": "Europe/Berlin" }例
この例は、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ライブラリは、日付や時刻のフォーマットをJavaScriptで解析、検証、操作、表示を可能にします。ネイティブで扱えない高度な日付解析やフォーマットが必要な場合のみ、プログラム可能な決定でmoment.jsライブラリを使ってください。それ以外の場合は、パフォーマンスを保つためにライブラリのロードを避けてください。
!注最適なパフォーマンスのためには、外部ライブラリを使用する際は パフォーマンスガイドライン を参照してください。
moment.jsライブラリを使用するには、プログラム可能な判定に以下のコードスニペットを含めてください。
load("classpath
.js");}
例
以下の例は、日付を解析し、出発日が1日前から365日前かの間かを判断する方法を示しています。
load("classpath
.js"); // to parse a date, eg. 2016-09-27T17:`50`.000Zvar 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の日付を特定のタイムゾーンに変換する必要がよくあります。以下の例は、moment-timezone.jsライブラリを使ってUTC日付を特定のタイムゾーンに変換する方法と、サマータイムを考慮した方法を示しています。両方のライブラリを正しい順序で読み込むようにしてください:
load("classpath
.js"); load("classpath.js");