setDate()

このエントリーをはてなブックマークに追加

setDate()はDateオブジェクトの日付を変更するメソッドです。

引数に1〜31の数値を指定するとその日付に変わります。

var d = new Date();
console.log(d.getDate());//19
d.setDate(10);
console.log(d.getDate());//10

マイナスの数値を設定するとその月の最終日から引いた日付が設定されます。

var d = new Date();
console.log(d.getDate());//19
d.setDate(-10);
console.log(d.getDate());//20

31より大きい値を設定した場合、31で割った余りが日付として設定され、月や年はそれにおおじて繰り上がります。

var d = new Date();
console.log(d.getDate());//19
console.log(d.getMonth());//9
d.setDate(32);
console.log(d.getDate());//1
console.log(d.getMonth());//10

登録日 : 2012年10月19日 最終更新日 : 2012年10月19日

同じカテゴリー(Date)のエントリー

検索

スポンサードリンク

バージョン

リファレンス