このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docsコミュニティーについてもっと知り、仲間になるにはこちらから。
Date.prototype.setTime()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
setTime() はDate インスタンスのメソッドで、この日付のタイムスタンプを変更します。これは、 UTC の 1970 年 1 月 1 日午前 0 時を元期として定義される、元期からのミリ秒数です。
In this article
試してみましょう
const launchDate = new Date("July 1, 1999, 12:00:00");const futureDate = new Date();futureDate.setTime(launchDate.getTime());console.log(futureDate);// 予想される結果: "Thu Jul 01 1999 12:00:00 GMT+0200 (CEST)"const fiveMinutesInMs = 5 * 60 * 1000;futureDate.setTime(futureDate.getTime() + fiveMinutesInMs);console.log(futureDate);// 予想される結果: "Thu Jul 01 1999 12:05:00 GMT+0200 (CEST)"// メモ: 時間帯は変更されることがあります。構文
js
setTime(timeValue)引数
timeValue新しいタイムスタンプを表す整数で、協定世界時 (UTC) 1970 年 1 月 1 日 00:00:00 からのミリ秒数です。
返値
その場でDate オブジェクトを変更し、新しいタイムスタンプを返します。timeValue がNaN (またはundefined など、NaN に変換されるその他の値)の場合、日付は無効な日時に設定され、NaN が返されます。
例
>setTime() の使用
js
const theBigDay = new Date("1999-07-01");const sameAsBigDay = new Date();sameAsBigDay.setTime(theBigDay.getTime());仕様書
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-date.prototype.settime> |