Date.prototype.setDate()
試してみましょう
setDate()
メソッドは、現在設定されている月から始まる Date
オブジェクトの「日」を設定します。
構文
dateObj.setDate(dayValue)
引数
dayValue
- 「日」を表す整数の値です。
返値
協定世界時 (UTC) 1970 年 1 月 1 日 00:00:00 から与えられた日付までの間のミリ秒単位の数値 (Date
オブジェクトも適切に変更されます)。
解説
dayValue
がその月の日付の範囲外の値の場合、それに応じて setDate()
が Date
オブジェクトを更新します。
例えば、dayValue
に 0 を与えた場合、日付は前月の最終日に設定されます。
dayValue
に負の数を指定すると、前月の最終日から逆算して日付が設定されます。 -1の場合、日付は前月の最終日の1日前に設定されます。
例
setDate() の使用
var theBigDay = new Date(1962, 6, 7); // 1962-07-07 (7th of July 1962)
theBigDay.setDate(24); // 1962-07-24 (24th of July 1962)
theBigDay.setDate(32); // 1962-08-01 (1st of August 1962)
theBigDay.setDate(22); // 1962-08-22 (22th of August 1962)
theBigDay.setDate(0); // 1962-07-31 (31th of July 1962)
theBigDay.setDate(98); // 1962-10-06 (6th of October 1962)
theBigDay.setDate(-50); // 1962-08-11 (11th of August 1962)
仕様書
ブラウザーの互換性
BCD tables only load in the browser