ES6 - setMonth() 方法



JavaScript 的 `setMonth()` 方法根據本地時間設定指定日期的月份。

語法

Date.setMonth(monthValue[, dayValue])       

引數

  • monthValue − 一個介於 0 和 11 之間的整數(代表 1 月到 12 月)。

  • dayValue − 一個介於 1 和 31 之間的整數,代表月份中的某一天。

  • msValue − 一個介於 0 和 999 之間的數字,代表毫秒。如果您指定了 `msValue` 引數,則也必須指定 `minutesValue` 和 `secondsValue`。

如果您沒有指定 `dayValue` 引數,則使用 `getDate` 方法返回的值。如果您指定的引數超出了預期範圍,`setMonth` 會嘗試相應地更新 Date 物件中的日期資訊。例如,如果您將 `monthValue` 使用 15,年份將加 1(年份 + 1),月份將使用 3。

示例

var dt = new Date( "Aug 28, 2008 23:30:00" ); 
dt.setMonth( 2 ); 
console.log( dt );      

輸出

Fri Mar 28 2008 23:30:00 GMT+0530 (India Standard Time)      
廣告
© . All rights reserved.