Date.prototype.setSeconds()
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.
setSeconds()
は Date
インスタンスのメソッドで、地方時に基づき、指定された日時の「秒」や「ミリ秒」を変更します。
試してみましょう
const event = new Date("August 19, 1975 23:15:30");
event.setSeconds(42);
console.log(event.getSeconds());
// 予想される結果: 42
console.log(event);
// 予想される結果: "Sat Apr 19 1975 23:15:42 GMT+0100 (CET)"
// Note: your timezone may vary
構文
js
setSeconds(secondsValue)
setSeconds(secondsValue, msValue)
引数
secondsValue
-
日付の「秒」を表す 0 から 59 までの間の整数値。
msValue
省略可-
日付の「ミリ秒」を表す 0 から 999 までの間の整数。
返値
解説
msValue
引数を指定しなかった場合、getMilliseconds()
メソッドから返される値が使われます。
指定した値が期待される日付の範囲外の場合、それに応じて setSeconds()
が Date
オブジェクトの日時情報の更新を試みます。
例えば、 secondsValue
に 100 を指定した場合、 Date
オブジェクトに格納された「分」に 1 が加算され、秒が 40 になります。
例
setSeconds() の使用
js
const theBigDay = new Date();
theBigDay.setSeconds(30);
仕様書
Specification |
---|
ECMAScript® 2026 Language Specification # sec-date.prototype.setseconds |