date.setMonth()是JavaScript中的內置函數,用於將月份設置為使用Date()構造函數創建的日期對象。
用法:
DateObj.setMonth(month_Value);
DateObj是使用Date()構造函數創建的有效Date對象,在其中我們要設置月份。month的值從0到11,因為一年中從1月到12月的月份總數是12。1的值是0, 2月1日,以此類推,直到12月11日。
參數:這裏的參數month_Value是要在使用Date()構造函數創建的日期中設置的月份值。
返回值:它返回由setMonth()函數設置的新的即更新的月份。
以下示例程序旨在說明setMonth()函數:
<script>
// Here a date has been assigned
// while creating Date object
var dateobj = new Date('October 13, 1996 05:35:32');
// new month of January is being set in above Date
// Object with the help of setMonth() function
dateobj.setMonth(0);
// new month from above Date Object is
// being extracted using getMonth()
var B = dateobj.getMonth();
// Printing new month
document.write(B);
</script>
輸出:
0
錯誤和異常
- 示例1:如果在Date()構造函數中創建日期對象時我們沒有給出任何月份,則setMonth()函數仍將能夠在創建的日期對象中設置新的月份。
<script> // Here month has not been assigned // while creating Date object var dateobj = new Date('1996, 05:35:32'); // new month of 2 is being set in above Date // Object with the help of setMonth() function dateobj.setMonth(2); // new month from above Date Object is // being extracted using getMonth() var B = dateobj.getMonth(); // Printing new month document.write(B); </script>
輸出:
2
- 示例2:如果在Date()構造函數中未提供任何參數,則setMonth()函數仍可以設置月份,但年份,日期等仍為當前值。
<script> // Here nothing has been assigned // while creating Date object var dateobj = new Date(); // new month of 11 is being set in above Date // Object with the help of setMonth() function dateobj.setMonth(11); // Month from above Date Object is // being extracted using getMonth() var B = dateobj.getMonth(); // date from above Date Object is // being extracted using getDate() var C = dateobj.getDate(); // year from above Date Object is // being extracted using getFullYear() var D = dateobj.getFullYear(); // Printing new month document.write(B + "<br>"); // Printing current date document.write(C + "<br>"); // Printing current year document.write(D); </script>
輸出:
11 1 2018
這裏11是12月的新月,1是當前日期,而2018是當前年份。
- 示例3:如果將月15的值作為setMonth()函數的參數給出,則它將3設置為月,因為月的範圍是0到11,因此(15%12 = 3)。
<script> // Here date has been assigned // while creating Date object var dateobj = new Date('October 13, 1996 05:35:32'); // new month of 15 is being set in above Date // Object with the help of setMonth() function dateobj.setMonth(15); // month from above Date Object is // being extracted using getMonth() var B = dateobj.getMonth(); // year from above Date Object is // being extracted using getFullYear() var C = dateobj.getFullYear(); // Printing new month document.write(B + "<br>"); // Printing new year document.write(C); </script>
輸出:
Here 3 is the new month of April and year becomes 1997 from 1996 because month range is form 0 to 11 i.e, total 12 and we set new month as 3 which increase year by one to 1997 from 1996 and month becomes 3.
支持的瀏覽器:JavaScript date.setMonth()函數支持的瀏覽器如下:
- 穀歌瀏覽器
- IE瀏覽器
- 火狐瀏覽器
- Opera
- 蘋果瀏覽器
相關用法
- Javascript Math.pow( )用法及代碼示例
- Javascript Array some()用法及代碼示例
- Javascript Number()用法及代碼示例
- Javascript Symbol.for()用法及代碼示例
- Javascript toExponential()用法及代碼示例
- Javascript toString()用法及代碼示例
- Javascript Math.abs( )用法及代碼示例
注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 JavaScript | date.setMonth() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。