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 July 2015.

setTime()Date インスタンスのメソッドで、この日付のタイムスタンプを変更します。これは、 UTC の 1970 年 1 月 1 日午前 0 時を元期として定義される、元期からのミリ秒数です。

試してみましょう

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 オブジェクトを変更し、新しいタイムスタンプを返します。 timeValueNaN (または 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

ブラウザーの互換性

関連情報