【JavaScript ECMAScript】Temporal::Temporal.ZonedDateTime.prototype.untilインスタンスメソッドの使い方
Temporal.ZonedDateTime.prototype.until()インスタンスメソッドの使い方について、初心者にもわかりやすく解説します。
基本的な使い方
Temporal.ZonedDateTime.prototype.until()メソッドは、特定のタイムゾーンにおける2つの日付と時刻の間の期間を計算するメソッドです。これは、ある時点から別の時点までの正確な時間差を、年、月、日、時間、分、秒、ミリ秒などの様々な時間単位で知りたい場合に非常に有用です。
このメソッドは、比較対象となる別のTemporal.ZonedDateTimeインスタンスを最初の引数として受け取ります。さらに、オプションとして設定オブジェクトを渡すことができ、これにより期間の計算に使用する最も大きな単位(例えば「年」や「月」)、期間の丸め方、オーバーフローの処理方法などを細かく指定することが可能です。
計算の結果として、2つのZonedDateTimeインスタンス間の期間を表すTemporal.Durationオブジェクトを返します。このDurationオブジェクトには、計算された各時間単位の値が格納されています。
Temporal.ZonedDateTime.prototype.until()は、タイムゾーンの概念を深く理解しており、サマータイムなどの複雑な時間調整も自動的に考慮して期間を計算します。これにより、グローバルに展開されるアプリケーションや、異なる地域間で正確な期間を扱う必要があるシステムにおいて、非常に信頼性の高い日付・時刻計算を実現します。現在Temporal APIはECMAScriptのプロポーザル段階ですが、将来的に標準となる強力な機能として注目されています。
構文(syntax)
1const startZonedDateTime = Temporal.ZonedDateTime.from("2023-01-01T00:00:00[America/New_York]"); 2const endZonedDateTime = Temporal.ZonedDateTime.from("2023-01-01T01:00:00[America/New_York]"); 3 4const duration = startZonedDateTime.until(endZonedDateTime);
引数(parameters)
other, options
- other: Temporal.ZonedDateTime: 比較対象となる別のTemporal.ZonedDateTimeオブジェクト
- options: Temporal.ZonedDateTimeUntilOptions | undefined: 差分計算のオプションを指定するオブジェクト(省略可能)
戻り値(return)
Temporal.Duration
このメソッドは、呼び出し元の Temporal.ZonedDateTime オブジェクトと、引数として渡された別の Temporal.ZonedDateTime オブジェクトとの間の期間を示す Temporal.Duration オブジェクトを返します。